如何使用JSON中的某个基本URL连接echo输出

时间:2013-05-16 03:30:05

标签: php json

我是下面的代码

<?php
 $movie_name="Dabangg 2";
$movie_name = urlencode($movie_name);
$url="http://api.themoviedb.org/3/search/movie?api_key=accd3ddbbae37c0315fb5c8e19b815a5&query=$movie_name";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ch);
curl_close($ch);
$obj = json_decode($response);
foreach ($obj->results as $results){
   echo $results->id;
   echo $results->"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92'.poster_path.'";
}
 ?>

和var_dump是

object(stdClass)#1 (4) {
  ["page"]=>
  int(1)
  ["results"]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (10) {
      ["adult"]=>
      bool(false)
      ["backdrop_path"]=>
      string(32) "/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg"
      ["id"]=>
      int(147405)
      ["original_title"]=>
      string(9) "Dabangg 2"
      ["release_date"]=>
      string(10) "2012-12-21"
      ["poster_path"]=>
      string(32) "/3tqT7N94fY73Ft6BVepnEqgwWyr.jpg"
      ["popularity"]=>
      float(0.46)
      ["title"]=>
      string(9) "Dabangg 2"
      ["vote_average"]=>
      float(5.8)
      ["vote_count"]=>
      int(2)
    }
  }
  ["total_pages"]=>
  int(1)
  ["total_results"]=>
  int(1)
}

我的代码工作正常。我只想使用基本网址poster_path检索http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92。我怎样才能做到这一点?以上 echo $results->"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92'.poster_path.'"; 只是我试图回显完整的URL。

输出URL echo应该是  http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/3tqT7N94fY73Ft6BVepnEqgwWyr.jpg

由于

3 个答案:

答案 0 :(得分:1)

除非我遗漏了某些东西......

echo 'http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92' . $results->poster_path;

答案 1 :(得分:1)

这可能会对您有所帮助:

    <?php

    $url_ahead = 'http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92';
    $poster_path = $results[0]['poster_path']; //or something like this
    //$poster_path =  "/3tqT7N94fY73Ft6BVepnEqgwWyr.jpg"; //for static
    // echo $url_ahead.''.$poster_path;
    echo $url_ahead.$poster_path;   

    ?>

答案 2 :(得分:1)

$baseUrl = "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92";
foreach ($obj->results as $results){
    echo $results[0]->id; //this also should be changed from $results->id
    $posterUrl = $results[0]['poster_path'];
    echo $baseUrl.$posterUrl; //Can store like $poster_path = $baseUrl.$posterUrl;
}

为什么需要使用$results[0]->id$results[0]['poster_path']?因为$results也是一个二维/多维数组。