如何在php中调用JSON URL,其中包括查询中的空格 [英] How to call the JSON URL in php which includes spaces in query

查看:105
本文介绍了如何在php中调用JSON URL,其中包括查询中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向tmdb.org发出xml请求时,我正在使用它

When I was making an xml request to tmdb.org I was using this

$movie_name="Dabangg 2";
    $xml = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/accd3ddbbae37c0315fb5c8e19b815a5/"$movie_name"");

它正在工作!!

现在我必须更改URL,因为响应是JSON而不是xml

Now I have to change the URL since the response is JSON not 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

在浏览器上,您将获得结果.

on browser you will get the results.

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 

请帮助我

推荐答案

查看以下内容: 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);
 ?>

这篇关于如何在php中调用JSON URL,其中包括查询中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆