0

当我向 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 

请帮我

4

2 回答 2

5

尝试

urlencode($movie_name);

当对要在 URL 的查询部分中使用的字符串进行编码时,此函数很方便,作为将变量传递到下一页的便捷方式。

于 2013-05-15T08:35:53.780 回答
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);
 ?>
于 2013-05-15T08:41:48.597 回答