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)
小智 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( "&", "&", 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)
Mic*_*les 20
file_get_contents()利用fopen()包装器,因此限制通过allow_url_fopenphp.ini中的选项访问URL .
您将需要更改您的php.ini以启用此选项或使用替代方法,即cURL - 迄今为止最流行,并且诚实地,标准方式来完成您要执行的操作.
我注意到你的URL中有空格.我认为这通常是一件坏事.尝试使用编码URL
$my_url = urlencode("my url");
Run Code Online (Sandbox Code Playgroud)
然后打电话
file_get_contents($my_url);
Run Code Online (Sandbox Code Playgroud)
看看你有没有更好的运气
您基本上需要通过请求发送一些信息.
试试这个,
$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)
这对我有用
我遇到了类似的问题。
由于超时!
超时可以这样表示:
$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)