我在从 PHP 代码调用 url 时遇到问题。我需要使用我的 PHP 代码中的查询字符串调用服务。如果我在浏览器中输入 url,它可以正常工作,但如果我使用 file-get-contents() 进行调用,我会得到:
Warning: file-get-contents(http://.... ) failed to open stream: HTTP request failed! HTTP/1.1 202 Accepted in ...
我使用的代码是:
$query=file_get_contents('http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
echo($query);
就像我说的 - 从浏览器调用,它工作正常。有什么建议吗?
我也尝试过使用另一个网址,例如:
$query=file_get_contents('http://www.youtube.com/watch?v=XiFrfeJ8dKM');
这很好用...可能是我需要调用的网址中有第二个 http://
吗?
最佳答案
尝试使用 cURL。
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
?>
关于PHP file_get_contents() 返回 "failed to open stream: HTTP request failed!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/697472/