2013-06-27 43 views
0

我想从相同的服务器获取文件,我正在运行我的php脚本,在其中我找到它。cUrl无法从相同的服务器获取文件

它不下载文件并获取超时。 我能够从浏览器使用相同的网址获取文件。

cUrl能够获取该文件,如果该url是除了相同的服务器以外的任何东西。

是否需要修改它们的任何设置以支持在同一台服务器上使用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); 
+0

是防火墙或NAT路由器后面的服务器?解析URL可能会返回公共IP,但您需要使用私有IP。 – Barmar

+0

直接从您的浏览器请求URL,看看会发生什么?该网址是否正常工作? –

+0

正如我所提到的那样,该URL起作用。我能够从浏览器中获取文件。 – kailash19

回答

0

尝试这个具有卷曲

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; 
} 
+0

不,它不工作。谢谢你的帮助。 – kailash19

0

使用谷歌浏览Copy as cURL所有参数(打开 “开发工具”, “网络” 选项卡上,单击右键请求)成功获取文件,粘贴到终端,然后执行。

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

另外,检查网络服务器日志,你甚至可以用脚本访问网络服务器吗?

您使用的是127.0.0.1localhost或完整的顶级域名?浏览器是否通过代理连接到互联网?

0

我找到的解决方法是这样的: 测试,如果你是你自己的服务器上,然后用一个简单的包括相同的脚本:

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