0

I am having a problem in my code

<?php
$con=mysqli_connect("localhost","test","test","test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"select movie_name from jos_movie");
while ($row = mysqli_fetch_array($result)){
        $movie_name= $row['movie_name'];
        $xmls = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/$movie_name");
        foreach($xmls->movies->movie as $movies){
        $arrMovie_id= $movies->id;  
      }
          echo $arrMovie_id;
    }
?>

If you see I am passing URL to fetch the results for movie ID . When the movie is of single name for example "Ironman" it fetches the correct movie Id "10505" and if the movie name is "Ironman 2' it wont work and gives wrong ID. But if I pass movie_name as "ironman%202" it fetches the correct result . How can I get the space problem fix . Since from DB it fetchs "ironman 2"

I have tried

while ($row = mysqli_fetch_array($result)){
        $movie_name= urlencode($row['movie_name']);
        $xmls = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/'.$movie_name);

and

while ($row = mysqli_fetch_array($result)){
        $movie_name= rawurlencode($row['movie_name']);
        $xmls = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/'.$movie_name);

its not working :(

Thanks

4

2 回答 2

1

您应该使用urlencode

$movie_name = urlencode($row['movie_name']);
$xmls = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/'.$movie_name);

编辑:

如果您只想获得第一个(且有效)获取的结果,请在 foreach 循环中的条件下中断(我想检查 id 是否为整数就足够了):

<?php
$con=mysqli_connect("localhost","test","test","test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"select movie_name from jos_movie");
while ($row = mysqli_fetch_array($result)){
        $movie_name = rawurlencode(trim($row['movie_name']));
        $xmls = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/$movie_name");
        foreach($xmls->movies->movie as $movies){
            $arrMovie_id = $movies->id;
            if ($arrMovie_id && is_int($arrMovie_id)) break;
        }
        echo $arrMovie_id;
    }
?>
于 2013-05-04T17:22:55.437 回答
0

您可以尝试以下方法:

$movie_name = urlencode($row['movie_name']);

那应该将空格转换为 %20。此外,如果电影标题中有任何非字母数字值,它们也应该被编码。

于 2013-05-04T17:22:30.623 回答