PHP擷取遠道檔案

來源:互聯網
上載者:User
PHP擷取遠程檔案
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!


------解決方案--------------------
C/C++ code
Bart Friederichs 16-Apr-2012 12:17file_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:30For 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 yyIf 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:16I 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.
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.