PHP获取远程文件

zhen3269656 2012-07-06 07:35:52
if(!($contents=file_get_contents('http://finance.yahoo.com/d/quotes.csv?s=AMZN&e=.csv&f=slldltlclohgv'))){
die('fail to open yahoo');
};
echo $contents;


为什么平均要刷新3-4下才有一次成功,其他都会出现一个警告,提示 HTTP request failed!
...全文
137 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
z109876543210 2012-07-07
  • 打赏
  • 举报
回复

function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){
if(function_exists("curl_init")){
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $url);
if(!empty($proxy["address"]))
@curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]);
if(!empty($proxy["account"]) && !empty($proxy["password"]))
@curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]);
@curl_setopt($ch, CURLOPT_REFERER, $referrer);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
@curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
@curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);
@curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
@curl_setopt($ch, CURLOPT_TIMEOUT, 1000);

if (!empty($post)) {
@curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}

$result = @curl_exec($ch);
@curl_close($ch);
}elseif(function_exists("fsockopen")){
$httpheader = "POST ".$url." HTTP/1.1\r\n";
$httpheader .= "Accept: */*\r\n";
$httpheader .= "Accept-Language: zh-cn\r\n";
$httpheader .= "Referer: ".$referrer."\r\n";
$httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
$httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
$httpheader .= "Host: ".$host."\r\n";
$httpheader .= "Content-Length: ".strlen($post)."\r\n";
$httpheader .= "Connection: Keep-Alive\r\n";
$httpheader .= "\r\n";
$httpheader .= $post;
$fd = fsockopen($host, 80);
fwrite($fd, $httpheader);
$result = "";
while(!feof($fd)){
$result .= fread($fd, 8192);
}
fclose($fd);
}elseif(function_existes('file_get_contents')){
$httpheader = "POST ".$url." HTTP/1.1\r\n";
$httpheader .= "Accept: */*\r\n";
$httpheader .= "Accept-Language: zh-cn\r\n";
$httpheader .= "Referer: ".$referrer."\r\n";
$httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
$httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
$httpheader .= "Host: ".$host."\r\n";
$httpheader .= "Content-Length: ".strlen($post)."\r\n";
$httpheader .= "Connection: Keep-Alive\r\n";

$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>$httpheader,
'content'=>$post
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, 'r', $context);
}
return $result;
}
qq120848369 2012-07-06
  • 打赏
  • 举报
回复
Bart Friederichs 16-Apr-2012 12:17
file_get_contents can do a POST, create a context for that first:

$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);

$context = stream_context_create($opts);
$url = 'https://'.$https_server;
$result = file_get_contents($url, false, $context, -1, 40000);
pperegrina 21-Dec-2011 11:30
For those having this problem when trying to get_file_contents(url):

Warning: file_get_contents(url): failed to open stream: HTTP request failed! in xx on line yy

If you are behind a SonicWall firewall, read this:
https://bugs.php.net/bug.php?id=40197
(this little line: uncheck a box in the internal settings of the firewall labled "Enforce Host Tag Search with for CFS")

Apparently by default SonicWall blocks any HTTP request without a "Host:" header, which is the case in the PHP get_file_contents(url) implementation.

This is why, if you try to get the same URL from the same machine with cURL our wget, it works.

I hope this will be useful to someone, it took me hours to find out :)
godwraith01 at yahoo dot com 11-Oct-2011 04:16
I experienced a problem in using hostnames instead straight IP with some server destinations.

If i use file_get_contents("www.jbossServer.example/app1",...)
i will get an 'Invalid hostname' from the server i'm calling.

This is because file_get_contents probably will rewrite your request after getting the IP, obtaining the same thing as :
file_get_contents("xxx.yyy.www.zzz/app1",...)

And you know that many servers will deny you access if you go through IP addressing in the request.

With cURL this problem doesn't exists. It resolves the hostname leaving the request as you set it, so the server is not rude in response.




这三条评论将会help you a lot,建议阅读后两条的原因后改用第一条的代码请求,另外记得给array里加一个Host:的字段,免得后面所说的Host被设置为IP的问题。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧