【问题标题】:connect to a port address via curl and php通过 curl 和 php 连接到端口地址
【发布时间】:2012-07-25 09:59:35
【问题描述】:

我正在使用 CURL 开发一个 php 站点,但它只能在我的本地服务器上运行,而不能在实时服务器上运行。它返回错误“无法连接到主机”

CURL iS 安装在实时服务器上,我已经对其进行了测试。

SSL 安装在实时服务器上,我正在使用 curl 连接到端口 9301

即;http://xx.xxx.xx.xxx:9301/test.

这是我的代码:

    $request =  'http://xx.xxx.xx.xxx:9301/test'; 
    // The request parameters 
    $context = 'something'; 

    // urlencode and concatenate the POST arguments 
    $postargs = 'xmlRequest='.urlencode($context);
    $session = curl_init($request); 
   // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true); 
    // Tell curl that this is the body of the POST
    curl_setopt($ch, CURLOPT_PORT, '9301');


    curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs); 
    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($session); 
   //echo curl_getinfo($session);   
    echo $err = 'Curl error: ' . curl_error($session);
   curl_close($session); 
   echo $response;

根据我的测试,很明显我无法通过 Curl 连接到端口地址。为了连接到端口地址,需要更改任何服务器设置吗?

【问题讨论】:

  • 您使用的是ssl,但您的地址以“http”开头?
  • 可能是您主机的防火墙阻止了该端口,您可能需要与他们核实一下。编辑:也可能是您的主机拒绝了没有用户代理的请求。

标签: php ssl curl


【解决方案1】:
curl_setopt($ch, CURLOPT_PORT, '9301');

那是你的问题。在您的情况下,句柄不是$ch,而是$session。

我猜只是一个错字。

一般:确保在开发过程中显示所有错误。记录它们或将它们放在屏幕上。 你会很容易抓住这个的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2019-06-19
    • 1970-01-01
    相关资源
    最近更新 更多