PHP file_get_contents()返回"无法打开流:HTTP请求失败!"

und*_*ned 81 php api file-get-contents query-string

我在从PHP代码调用url时遇到问题.我需要使用PHP代码中的查询字符串来调用服务.如果我在浏览器中键入url,它可以正常工作,但如果我使用file-get-contents()来进行调用,我会得到:

警告:file-get-contents(http:// ....)无法打开流:HTTP请求失败!HTTP/1.1 202接受于......

我使用的代码是:

$query=file_get_contents('http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
echo($query);
Run Code Online (Sandbox Code Playgroud)

就像我说 - 从浏览器调用,它工作正常.有什么建议?

我也尝试过另一个网址,例如:

$query=file_get_contents('http://www.youtube.com/watch?v=XiFrfeJ8dKM');
Run Code Online (Sandbox Code Playgroud)

这工作得很好......可能是我需要调用的网址中有第二个http://吗?

Jam*_*all 104

尝试使用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);

?>
Run Code Online (Sandbox Code Playgroud)

  • 当真正的问题出现在&符号中时,这太复杂了. (10认同)
  • 不是每个人都安装了(但应该)cURL.cURL肯定要快很多倍,但file_get_contents也不是*那么慢,并且不要求你每次使用它都记住所有选项. (4认同)
  • @Christian,你能详细说明一下吗? (2认同)

小智 21

<?php

$lurl=get_fcontent("http://ip2.cc/?api=cname&ip=84.228.229.81");
echo"cid:".$lurl[0]."<BR>";


function get_fcontent( $url,  $javascript_loop = 0, $timeout = 5 ) {
    $url = str_replace( "&amp;", "&", urldecode(trim($url)) );

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302) {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

        if ( $headers = get_headers($response['url']) ) {
            foreach( $headers as $value ) {
                if ( substr( strtolower($value), 0, 9 ) == "location:" )
                    return get_url( trim( substr( $value, 9, strlen($value) ) ) );
            }
        }
    }

    if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) && $javascript_loop < 5) {
        return get_url( $value[1], $javascript_loop+1 );
    } else {
        return array( $content, $response );
    }
}


?>
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的代码,看起来像你得到它:http://php.net/manual/en/ref.curl.php的主要问题是函数调用`get_url`实际上应该`get_fcontent`因为你改了名字功能本身.这实际上是一个递归函数调用,它通过更改某些参数来重新尝试获取URL内容. (5认同)

Mic*_*les 20

file_get_contents()利用fopen()包装器,因此限制通过allow_url_fopenphp.ini中的选项访问URL .

您将需要更改您的php.ini以启用此选项或使用替代方法,即cURL - 迄今为止最流行,并且诚实地,标准方式来完成您要执行的操作.


sli*_*lim 7

我注意到你的URL中有空格.我认为这通常是一件坏事.尝试使用编码URL

$my_url = urlencode("my url");
Run Code Online (Sandbox Code Playgroud)

然后打电话

file_get_contents($my_url);
Run Code Online (Sandbox Code Playgroud)

看看你有没有更好的运气


d_b*_*gar 7

您基本上需要通过请求发送一些信息.

试试这个,

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n")); 
//Basically adding headers to the request
$context = stream_context_create($opts);
$html = file_get_contents($url,false,$context);
$html = htmlspecialchars($html);
Run Code Online (Sandbox Code Playgroud)

这对我有用

  • 超级答案。投票赞成将其提升至顶部。全部在 php 中,也适用于 https 请求。只需额外两行代码。如果共享服务器确实提供curl怎么办?这非常有帮助。 (3认同)
  • 这对我也有用!看起来需要用户代理 (2认同)

Jer*_*rry 7

我遇到了类似的问题。

由于超时!

超时可以这样表示:

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => "POST",
        'content' => http_build_query($data2),
        'timeout' => 30,
    ),
);
$context = stream_context_create($options);
$retour = @file_get_contents("http://xxxxx.xxx/xxxx", false, $context);
Run Code Online (Sandbox Code Playgroud)