【发布时间】: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 忽略或不需要防火墙允许。