当我向tmdb.org发出xml请求时,我正在使用此
$movie_name="Dabangg 2";
$xml = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/accd3ddbbae37c0315fb5c8e19b815a5/"$movie_name"");
它正在工作!!
现在我必须更改URL,因为响应是JSON而不是xml
我已将其更改为
<?php
$movie_name="Dabangg 2";
$url="http://api.themoviedb.org/3/search/movie?api_key=accd3ddbbae37c0315fb5c8e19b815a5&query=$movie_name";
$json = file_get_contents($url);
var_dump($json);
?>
但它不起作用
如果您搜索
http://api.themoviedb.org/3/search/movie?api_key=accd3ddbbae37c0315fb5c8e19b815a5&query=Dabangg 2
在浏览器上,您将获得结果。
Warning: file_get_contents(http://api.themoviedb.org/3/search/movie?api_key=accd3ddbbae37c0315fb5c8e19b815a5&query=Dabangg 2) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST in /home/wwww/public_html/test/movie.php on line3
请帮帮我
答案 0 :(得分:5)
尝试
urlencode($movie_name);
当编码要在a中使用的字符串时,此功能很方便 查询URL的一部分,作为将变量传递给下一个的便捷方式 页。
答案 1 :(得分:1)
请看:http://codular.com/curl-with-php
尝试以下代码:
<?php
$movie_name="Dabangg 2";
$movie_name = urlencode($movie_name);
$url="http://api.themoviedb.org/3/search/movie?api_key=accd3ddbbae37c0315fb5c8e19b815a5&query=$movie_name";
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump($resp);
?>