0

我有一个表结构,我试图在插入查询之前从 moviedb.org 获取 movie_id。

在此处输入图像描述

这是代码的一部分

      $movie_name = $arrTitle[0];

    $apiKey = '30f44b6ef9472d414e50d2acaa058b60';
    $url = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/30f44b6ef9472d414e50d2acaa058b60/"$movie_name"");
    $xml = simplexml_load_file($url);
    $movies = $xml->movies->movie;
    $images = $xml->xpath("//image");
   foreach ($movies as $movie){
        $arrMovie_id = $movie->id;
    }
    foreach ($images as $image){
    $attr = $image->attributes();
     if($image['size'] == "thumb" && $image['type']=="poster"){       
        $images= $image['url'];
        }
    }
    $arrStr = explode(':',$htmlShowTime);
    $release = substr($arrStr[3],0,strlen($arrStr[3])-8);
    $director = substr($arrStr[5],0,strlen($arrStr[5])-11); 

    $sql_movie = "insert into jos_movie(movie_name,language,cast,movie_release,director,rating,rating_count,movie_ids,image)values('$movie_name','null','$cast','$release','$director',250,230,'$arrMovie_id','$images')";
    //echo $sql.'<br>';
   // echo $sql_movie;

    mysql_query($sql_movie);

如您所见,我试图从中获取 movie_id$url但在图像字段中获取 0 和某些情况下的“Array”

例如第一部电影“Go Goa Gone”

网址是http://api.themoviedb.org/2.1/Movie.search/en/xml/30f44b6ef9472d414e50d2acaa058b60/%22Go%20Goa%20Gone%22

它应该获取 id 和 image

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query searchTerms="&quot;Go+Goa+Gone&quot;"/>
  <opensearch:totalResults>1</opensearch:totalResults>
  <movies>
    <movie>
      <score>42.34326</score>
      <popularity>3</popularity>
      <translated>true</translated>
      <adult>false</adult>
      <language>en</language>
      <original_name>Go Goa Gone</original_name>
      <name>Go Goa Gone</name>
      <alternative_name></alternative_name>
      <type>movie</type>
      <id>191562</id>
      <imdb_id>tt2436516</imdb_id>
      <url>http://www.themoviedb.org/movie/191562</url>
      <votes>0</votes>
      <rating>0.0</rating>
      <certification>NR</certification>
      <overview>A rave party off the coast of Goa, goes horrifyingly and hilariously wrong when the island is overrun with zombies.</overview>
      <released>2013-05-10</released>
      <images>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="thumb" width="92" height="133" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="w154" width="154" height="222" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="cover" width="185" height="267" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="w342" width="342" height="493" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="mid" width="500" height="721" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="original" width="810" height="1168" id="51880dd7760ee3144531d0b9"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="thumb" width="300" height="169" id="51880e1f760ee3059e36a012"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="poster" width="780" height="439" id="51880e1f760ee3059e36a012"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="w1280" width="1280" height="720" id="51880e1f760ee3059e36a012"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="original" width="1280" height="720" id="51880e1f760ee3059e36a012"/>
      </images>
      <version>1</version>
      <last_modified_at>2013-05-06 20:14:04 UTC</last_modified_at>
    </movie>
  </movies>
</OpenSearchDescription>

这是 var_dump 的$movie_name

string(12) "Go Goa Gone "
string(6) "Gippi "
string(19) "Shootout at Wadala "
string(23) "Lucky Di Unlucky Story "
string(11) "Aashiqui 2 "

对于这种不规则性,我在代码中哪里错了?

4

1 回答 1

1

您需要在找到多个图像的 for 循环中执行这样的操作。我在 XML 文件中创建了多个电影列表的列表并测试了下面的代码。

//$movies = $xml->movies;
    $movies = $xml->xpath("//movies/descendant::movie");
    //echo "<pre>";print_r($movies);die;
    //$images = $xml->xpath("//image");
    foreach ($movies as $movie){
        echo $arrMovie_id = $movie->id;
        $images = $movie->xpath("//image");
        //echo "<pre>";print_r($images);
            $imageUrl = array();
        foreach ($images as $image){
            if($image['size'] == "thumb" && $image['type']=="poster"){       
                $imageUrl[] = $image['url'];
            }
        }

        //then you need to implode $imageUrl array to string so that you can add multiple found image urls
        $imageUrl = implode(",", $imageUrl);

        $arrStr = explode(':',$htmlShowTime);
        $release = substr($arrStr[3],0,strlen($arrStr[3])-8);
        $director = substr($arrStr[5],0,strlen($arrStr[5])-11); 

        $sql_movie = "insert into jos_movie(movie_name,language,cast,movie_release,director,rating,rating_count,movie_ids,image)values('$movie_name','null','$cast','$release','$director',250,230,'$arrMovie_id','$imageUrl')";
        //echo $sql.'<br>';
        echo $sql_movie;

        //mysql_query($sql_movie);
        echo "<hr/>";
    }

编辑:imageUrl 在 sql 查询中的代码 $imageUrl 中创建为数组,而不是 $images(它是 XML 数组,因此不会使用)

从上面的代码中,我可以在表格中插入多部电影,见下图 多部电影列表

于 2013-05-12T03:33:41.330 回答