cUrl无法从同一服务器获取文件

时间:2013-06-27 10:58:08

标签: php curl

我正在尝试从同一台服务器上获取文件我正在运行我的php脚本,我可以在其中获取它。

它不下载文件并获得超时。 我可以使用浏览器中的相同网址获取文件。

如果网址不是同一台服务器,则cUrl能够获取文件。

我需要修改他们的任何设置以支持在同一台服务器上使用cUrl下载文件。

在此感谢您的帮助。

我的代码:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $file_url);
    $content = curl_exec($ch);
    curl_close($ch);

3 个答案:

答案 0 :(得分:0)

尝试使用curl的所有参数

function curl_download($Url){

// is cURL installed yet?
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

// 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, "http://www.example.org/yay.htm");

// 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);

// Download the given URL, and return output
$output = curl_exec($ch);

// Close the cURL resource, and free system resources
curl_close($ch);

return $output;
}

答案 1 :(得分:0)

在成功获取的文件上使用Google Chrome“Copy as cURL(打开”开发工具“,”网络“标签,右键单击请求),粘贴到终端,然后执行。

如果一切正常,并且您可以通过终端的curl获取文件,那么有一些标题和/或请求参数。如果没有,那么您的网络配置可能出现问题。

另外,检查Web服务器日志,您是否使用脚本访问Web服务器?

您使用的是127.0.0.1localhost还是完整的TLD?浏览器是否通过代理连接到Internet?

答案 2 :(得分:0)

我找到的解决方法是: 测试您是否在自己的服务器上,然后对同一个脚本使用简单的包含:

    if (($ser = servername()) != $floc) {
       return(GetFileContent("$floc/$page.php"));
    } else { include "$page.php"; }