0

我正在尝试使下面的代码与 curl 或其他东西一起使用。我已经看过 curl fsockopen 转换,但它不起作用。除了在我的主机上禁用 fsockopen 之外,该代码有效。任何帮助将不胜感激。

$host = substr($hostport,0,50);
$port = substr($hostport,50,10);
$issecure = substr($hostport,60,1);

if($issecure=="Y")
{
    $host = "ssl://".$host;
}
$fsok = fsockopen(trim($host) , intval(trim($port))); 
if(FALSE == $fsok )
{
    echo "Target Host not Found/Down"; return ;
}
fwrite($fsok, $bodyData ); 
$port ='';$host ='';$hostport= '';$bodyData='';

while ($line = fread($fsok, 25000))
{
    echo $line;
}

fclose($fsok); 
return $line;
4

1 回答 1

1

假设您使用 http/https 作为协议,如下所示:

$client = curl_init();

$options = array(
  CURLOPT_PORT => $port
  CURLOPT_RENTURNTRANSFER => true,
  CURLOPT_SSL_VERIFYPEER => false, // dont verify the cert
  CURLOPT_URL => ($issecure ? 'https://' : 'http://'). $host
);

$response = curl_exec($client);

if(false !== $response) {
  echo $response;
  return $response;
} else {
  if($error = curl_error($client)) {
    echo $error;
    return $error;
  } else {
    echo 'Something is wrong. Error could not be determined.';
  }
}

另一个很棒且更易于使用的选项是Zend_Http_Client它支持fsockopencurl因此您可以很容易地来回切换而无需修改代码。

于 2010-10-05T06:05:34.037 回答