【问题标题】:Any other good option for fsockopen?fsockopen 还有其他好的选择吗?
【发布时间】:2011-04-21 03:19:47
【问题描述】:

我正在尝试使下面的代码与 curl 或其他东西一起使用。我已经看过 curl fsockopen conversion 但它不起作用。除了在我的主机上禁用 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;

【问题讨论】:

  • 我该怎么做?真的不明白你的期望..

标签: php curl fopen fsockopen


【解决方案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,因此您无需修改​​代码即可轻松地来回切换。

【讨论】:

  • 抱歉,回复晚了,正在尝试查看它是否可以工作,但它正在写入未找到目标主机..请我为您粘贴完整的代码..谢谢
猜你喜欢
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多