我正在完成我的脚本,突然libcurl停止工作。我以为我连接的服务器已关闭或者其他东西,但总是在我尝试连接的地方我得到同样的sucky错误。我甚至尝试重新启动我的盒子,结果相同。事情发生的代码片段是:
public function navigate(array $data)
{
$ch = curl_init();
// Setup cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_INTERFACE, $data['ip']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookies.jar");
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookies.jar");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $data['agent']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Visit homepage
curl_setopt($ch, CURLOPT_URL, "SOMEWEBSITEHERE, MAKES NO DIFFERENCE");
$page= curl_exec($ch);
var_dump($page, curl_error($ch), curl_errno($ch));
}
以上var_dump()将产生此输出(使用www.yahoo.com作为URL):
bool(false)
string(51) "Failed to connect to 69.147.76.15: Unknown error 22"
int(7)
Login failed!
我甚至尝试用wireshark嗅探连接,cURL甚至没有发出连接请求;只有DNS请求。
我现在很无能为力。
我真的很感激这一方面。
感谢。
答案 0 :(得分:1)
这是我的完整(工作)代码(基本上是你的,但有更多的输出),希望它会有所帮助:
$url = 'www.yahoo.com';
$data['ip'] = $_SERVER['SERVER_ADDR'];
$cookie_jar = sys_get_temp_dir() . 'cookiejar.txt';
$data['agent'] = 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8';
$fp = fopen('php://temp', 'r+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_INTERFACE, $data['ip']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $data['agent']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $fp);
$page = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
rewind($fp);
$verbose = fread($fp, 8192);
fclose($fp);
echo '<pre>';
echo 'Error number: ' . $errno . '<br />';
echo 'Error message: ' . $errmsg . '<br />';
echo '------------------' . '<br />';
print_r($info);
echo '------------------' . '<br />';
echo $verbose . '<br />';
echo '------------------' . '<br />';
echo htmlspecialchars($page);
echo '</pre>';