【发布时间】:2014-05-06 22:08:22
【问题描述】:
我看过之前关于 cURL 和 HTTP 代码 0 的帖子,但它们没有帮助。
我可以使用下面的脚本从localhost 卷曲到www.bambooping.com - 即localhost 上的test_curl.php 调用bambooping.com 上的test_curl2.php。但是,如果我在 bambooping.com 上运行它,我会得到 HTTP 代码 0。(我知道在同一主机上调用它是愚蠢的 - 这只是为了隔离问题。)
在bambooping.com safe_mode 上没有设置,并且curl 被编译进来(也就是说,应该是因为我可以cURL 进来)。这很奇怪 - 调用主机正在阻止 cURL。为什么用 cURL 调用会这样失败,而用 cURL 调用同一台主机就可以了?
test_curl.php:
<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
// curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
// make it blank - then it is ignored - otherwise, checked and error returned!
curl_setopt($ch, CURLOPT_REFERER, '');
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Download the given URL, and return output
$output = curl_exec($ch);
print_r(curl_getinfo($ch));
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
$str = curl_download("http://www.bambooping.com/test_curl2.php");
echo $str;
?>
test_curl2.php
<?php
echo "I am here";
?>
curl_getinfo 是:
Array
(
[url] => http://www.bambooping.com/test_curl2.php
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 4.3E-5
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
)
想法?我刚出... 谢谢 -
【问题讨论】: