【问题标题】:Unable to make php curl request with port number无法使用端口号发出 php curl 请求
【发布时间】:2011-11-03 13:50:47
【问题描述】:

我无法使用端口号发出 php curl 请求,没有端口号我得到正确响应。

当我 curl http://www.sample.com:8088 时,通过终端我得到了正确的回复。但不是通过 php curl 做 curl_inf() 我得到了

Array ( [url] => http://www.sample.com:8088/ [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => 0 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) ) 

我的代码

$url = "http://www.sample.com:8088/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$report=curl_getinfo($ch);
print_r($report);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

此代码给我来自 ubuntu 的响应,但如果指定了端口号,则不给我来自 cent os 的响应。

请告诉我如何解决它。

提前致谢。

【问题讨论】:

    标签: php curl port


    【解决方案1】:

    尝试这样设置端口:

    curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);
    

    或:

    curl_setopt($ch, CURLOPT_PORT, 8088);
    

    【讨论】:

      【解决方案2】:

      “setsebool httpd_can_network_connect on”为我解决了这个问题

      【讨论】:

      • 好的,但是为什么呢?请在您的答案中添加某种解释,以便其他人知道为什么这对您有用。
      【解决方案3】:

      我有这个问题去了服务器管理员,他在centos中更改了配置

      在文件夹 /etc/sysconfig/ 中

      他编辑了文件 selinux 并更改了 SELINUX=disabled 并重新声明了它。

      # This file controls the state of SELinux on the system.
      # SELINUX= can take one of these three values:
      #     enforcing - SELinux security policy is enforced.
      #     permissive - SELinux prints warnings instead of enforcing.
      #     disabled - No SELinux policy is loaded.
      SELINUX=disabled
      

      我的问题解决了。

      【讨论】:

      • 禁用整个 SE Linux 只是为了让 curl 接受 URL 中的端口号不是一个解决方案,最好是临时解决方法,但从长远来看绝对不是。
      • 这不应该是一个公认的答案。马里奥的回答应该足够了。
      【解决方案4】:

      确保在 Cent OS 中启用了 8080 端口

      试试这个curl_setopt($ch, CURLOPT_PORT, 8088);

      【讨论】:

        【解决方案5】:

        添加

        curl_setopt($ch, CURLOPT_PORT, 8088);
        

        Curl_setopt参考

        【讨论】:

        • 我试过 curl_setopt($ch, CURLOPT_PORT, 8088);也是一样的结果。我也试过删除 curl_setopt($ch, CURLOPT_PORT, 8088);
        【解决方案6】:

        尝试设置

        curl_setopt($ch,CURLOPT_PORT, 8088);
        

        查看更多信息http://php.net/manual/en/function.curl-setopt.php

        【讨论】:

          猜你喜欢
          • 2022-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-29
          • 2016-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多