【问题标题】:CURL response return null while use http proxy PHP使用 http 代理 PHP 时 CURL 响应返回 null
【发布时间】:2019-12-23 07:18:19
【问题描述】:

我正在尝试使用 CURL + PROXY 调用 API,我正在为我的 VPS 使用 virtualmin 管理面板。我得到空标题,空方盒子,@ 987654321@ 这个问题发生在 virtualmin 管理面板中,在此之前我使用 cpanel,在 cpanel 中一切正常,当我使用 Shock4 代理时,我只得到代理的响应(我删除了配置代码)。

    $ch = curl_init("https://www.example.com");
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_VERBOSE, false);

    if($proxy) 
    {
        $splited = explode(':',$proxy); // Separate IP and port
        curl_setopt($ch, CURLOPT_PROXY, $splited[0]);
        curl_setopt($ch, CURLOPT_PROXYPORT, $splited[1]); 
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    }
    if($cookie) curl_setopt($ch, CURLOPT_COOKIE, 'cookie');

    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch);
    if(!$httpcode) return false; else{
        $header = substr($response, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
        $body = substr($response, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
        curl_close($ch);
        echo 'Curl error: ' . curl_error($ch);
        return array($header, $body);
    } 

我认为某些防火墙阻止了此请求 我对linux没有太多了解,我使用的是ubuntu 18.*

【问题讨论】:

  • 代理可能很慢,您可以尝试增加超时时间。
  • 您忘记了$ 符号吗? if($cookie) curl_setopt($ch, CURLOPT_COOKIE, cookie); 在行尾?
  • if(!$httpcode) return false; else{ 是无效的语法。您应该将return false; 括在大括号中。
  • @MarkusZeller 代理是高速且工作的,当我切换到 virtualmin 管理面板 http 代理不再工作时,我在具有相同 ip 的 cpanel 上对其进行了检查。我认为某种防火墙会阻止它。我正在使用 php 7.0,因为 http 代理必须与防火墙一起使用,就像我提到的Shock4 代理一样,因为Shock4 忽略或不需要防火墙允许。

标签: php curl libcurl php-curl


【解决方案1】:

我喜欢自己回答: 在 Virtualmin 管理面板中,转到->webmin->networking->单击添加端口->设置单个端口或在范围内设置端口->保存设置然后单击应用配置然后重新启动您的机器;

它对我有用。

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 2022-01-17
    • 2017-11-25
    • 2018-09-11
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多