Google Api收集Php数组中的结果()

时间:2011-12-10 17:16:36

标签: php api google-shopping-api

我在网址中运行此内容:

https://www.googleapis.com/shopping/search/v1/public/products?key=thekey&country=US&q=xbox+360&alt=json

这给我的输出如下:

{
 "kind": "shopping#products",
 "etag": "\"czKOfew9E3svi7vOBQ3vsAgGZzo/s56qwZYTznZuaaCXzw1weX_JANI\"",
 "id": "tag:google.com,2010:shopping/products",
 "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=xbox+360&alt=json&startIndex=1&maxResults=25",
 "nextLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=xbox+360&alt=json&startIndex=26&maxResults=25",
 "totalItems": 203461,
 "startIndex": 1,
 "itemsPerPage": 25,
 "currentItemCount": 25,
 "items": [
  {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/1113342/8881409096328278418",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/8881409096328278418?alt=json",
   "product": {
    "googleId": "8881409096328278418",
    "author": {
     "name": "Walmart",
     "accountId": "1113342"
    },
    "creationTime": "2011-11-13T21:18:07.000Z",
    "modificationTime": "2011-12-08T01:12:50.000Z",
    "country": "US",
    "language": "en",
    "title": "More Facts On Xbox 360 4gb Capabilities",
    "description": "Xbox 360 4GB ConsoleBuilt-in Wi-Fi: The new Microsoft Xbox 360 console is the only console with 802.11n Wi-Fi built-in for a faster and easier connection to Xbox LIVE. Stream HD movies and TV or download games from Xbox LIVE in 1080p and 5.1 surround sound from anywhere in the house. Compatible with b/g/n networks.Xbox LIVE: Xbox LIVE brings a world of entertainment to you. Instantly watch movies and TV streaming from Netflix. In crystal-clear HD, or download game add-ons like new levels, characters, maps and songs. Easily jump right into online multiplayer fun and play along with friends around the world. Plus, get your favorite music from Last.fm, connect with friends on Facebook and post Tweets on Twitter. Xbox LIVE is your connection to more games, entertainment and fun.Kinect-Ready: Featuring a dedicated, specialized port, this Microsoft Xbox 360 console is built to connect seamlessly with the Kinect Sensor. Kin",
    "link": "http://www.walmart.com/catalog/product.do?product_id=14917960&sourceid=1500000000000003142040&ci_src=14110944&ci_sku=14917960",
    "brand": "Microsoft",
    "condition": "new",
    "gtin": "00885370138405",
    "gtins": [
     "00885370138405"
    ],
    "inventories": [
     {
      "channel": "online",
      "availability": "inStock",
      "price": 199.0,
      "shipping": 7.97,
      "currency": "USD"
     }
    ],
    "images": [
     {
      "link": "http://i.walmartimages.com/i/p/00/88/53/70/13/0088537013840_500X500.jpg"
     }
    ]
   }
  },
  {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/10046/10100715335711953213",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/10046/gid/10100715335711953213?alt=json",
   "product": {
    "googleId": "10100715335711953213",
    "author": {
     "name": "Target",
     "accountId": "10046"
    },
    "creationTime": "2011-08-23T19:06:38.000Z",
    "modificationTime": "2011-12-09T22:37:12.000Z",
    "country": "US",
    "language": "en",
    "title": "Xbox 360 4gb Console",
    "description": "The new Xbox 360 4GB Console. Here today, ready for tomorrow with a brand new, leaner machine. Wi-Fi is built-in for easier connection to the world of entertainment on Xbox LIVE, where HD movies and TV stream in an instant. It s ready for the controller-free experiences of Kinect you don t just play the game, you are the game. Xbox 360 is more games, entertainment and fun. What s In The Box? Xbox 360 4GB S Console Xbox 360 Black Wireless Controller Xbox 360 Composite A/V cable (standard definition) One Month Xbox LIVE Gold Membership 1 year limited warranty on console",
    "link": "http://www.target.com/p/Xbox-360-4GB-Console/-/A-12885804?ref=tgt_adv_XSG10001&AFID=Froogle_df&LNM=%7C12885804&CPNG=video%20games&ci_src=14110944&ci_sku=12885804",
    "brand": "Microsoft",
    "condition": "new",
    "gtin": "00885370138405",
    "gtins": [
     "00885370138405"
    ],
    "inventories": [
     {
      "channel": "online",
      "availability": "inStock",
      "price": 199.99,
      "shipping": 12.85,
      "currency": "USD"
     }
    ],
    "images": [
     {
      "link": "http://img3.targetimg3.com/wcsstore/TargetSAS/img/p/12/88/12885804.jpg"
     }
    ]
   }
  },
  {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/1161353/4408757490534070643",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1161353/gid/4408757490534070643?alt=json",
   "product": {
    "googleId": "4408757490534070643",
    "author": {
     "name": "Buy.com",
     "accountId": "1161353"
    },
    "creationTime": "2011-04-24T04:06:32.000Z",
    "modificationTime": "2011-12-09T06:24:51.000Z",
    "country": "US",
    "language": "en",
    "title": "Xbox 360 System (4GB)",
    "description": "Product Description The new Xbox 360. Here today, ready for tomorrow with a brand new, leaner machine in an all new black gloss finish. Wi-Fi is built-in for easier connection to the world of entertainment on Xbox LIVE, where HD movies and TV stream in an instant. Kinect Ready Featuring a dedicated, specialized port, the Xbox 360 4GB is built to connect seamlessly with the Kinect Sensor. Kinect brings games and entertainment to life in extraordinary new ways no controller required. Easy to use and instantly fun, Kinect gets everyone off the couch moving, laughing and cheering. See a ball? Kick it. Want to join a friend in the fun? Simply jump in. Even control an HD movie with a wave of your hand. Xbox LIVE Xbox LIVE brings a world of entertainment to you. Instantly watch movies and TV streaming from Netflix. In crystal-clear HD, or download game add-ons like new levels, characters, maps and songs. Easily jump right into online multiplayer fun and play along with friends around the world. Plus, get your favorite music from Last.fm and your Zune Pass, connect with friends on Facebook and post Tweets on Twitter. Xbox LIVE is your connection to more games, entertainment and fun. Looking for more than just a video game system? Just connect your Digital Camera, iPod, Zune, or other MP3 player via USB cable and immediately begin enjoying all your favorite Music and Movies on the TV that your Xbox 360 is hooked up to. It plays DVDs and CDs and you can even download the latest movies and TV shows via Xbox Live. It is a full Windows Media Extender and can connect with other PCs around the house to stream your favorite media. What Does Xbox Live Mean or Do? Join Xbox Live to have full access to the Marketplace where you can download full Games, Demos, Trailers, Movies and more. Voice, Text, and Video chat with friends and even play games together online (Note: multiplayer gaming requires Xbox Live Gold subscription). Xbox 360 Games To Watch Out For Gears of War 3 Call of Duty: Modern Warfare 3 Assassin's Creed: Revelations Tomb Raider Battlefield 3 Deus Ex: Human Revolution Child of Eden Xbox 360 Wireless Controller (black) This award-winning, high-performance wireless controller features a range of up to 30 feet and a battery life of up to 30 hours on two AA batteries. Built-in Wi-Fi The new Xbox 360 is the only console with 802.11n Wi-Fi built-in for a faster and easier connection to Xbox LIVE. Stream HD movies and TV or download games from Xbox LIVE in 1080p and 5.1 surround sound from anywhere in the house. Compatible with b/g/n networks.",
    "link": "http://clickfrom.buy.com/default.asp?adid=17379&sURL=http%3A%2F%2Fwww.buy.com%2Fprod%2Fxbox-360-system-4gb%2F216907636.html",
    "brand": "Microsoft",
    "condition": "new",
    "gtin": "00885370138405",
    "gtins": [
     "00885370138405"
    ],
    "inventories": [
     {
      "channel": "online",
      "availability": "inStock",
      "price": 199.99,
      "shipping": 0.0,
      "currency": "USD"
     }
    ],
    "images": [
     {
      "link": "http://ak.buy.com/PI/0/1000/216907636.jpg"
     }
    ]
   }
  },
  {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/1113342/2934483943836745321",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/2934483943836745321?alt=json",
   "product": {
    "googleId": "2934483943836745321",
    "author": {
     "name": "Walmart",
     "accountId": "1113342"
    },
    "creationTime": "2011-03-25T21:44:47.000Z",
    "modificationTime": "2011-12-08T01:12:50.000Z",
    "country": "US",
    "language": "en",
    "title": "Xbox 360 250gb Console",
    "description": "The new Xbox 360 250GB console. It's here today and ready for tomorrow, with a brand-new, leaner machine in an all-new black gloss finish. Wi-Fi is built in for easier connection to the world of entertainment on Xbox LIVE, where HD movies and TV stream in an instant. It's ready for the controller-free experiences of Kinect you don't just play the game; you are the game. Kinect brings games and entertainment to life in extraordinary new ways no controller required. Easy to use and instantly f",
    "link": "http://www.walmart.com/catalog/product.do?product_id=14683340&sourceid=1500000000000003142040&ci_src=14110944&ci_sku=14683340",
    "brand": "Microsoft",
    "condition": "new",
    "gtin": "00885370127119",
    "gtins": [
     "00885370127119"
    ],
    "inventories": [
     {
      "channel": "online",
      "availability": "inStock",
      "price": 299.0,
      "shipping": 7.97,
      "currency": "USD"
     }
    ],
    "images": [
     {
      "link": "http://i.walmartimages.com/i/p/00/88/53/70/12/0088537012711_500X500.jpg"
     }
    ]
   }
  }

我想从price获取所有值并将它们存储到php中的数组中?如何在somepage.php上运行此查询并将json值输出到该页面中的php数组中?

1 个答案:

答案 0 :(得分:1)

此问题的解决方案,步骤:

  1. 检索远程源文本
  2. 将JSON字符串解码为数组
  3. 迭代生成的数组以检索所需的数据。

  4. 有多种方法可以在php中检索远程文件/文本。最突出的是......

    使用file_get_contents()

    $txt = file_get_contents('http://www.example.com/');
    echo $txt;
    

    PHP的卷曲功能:

    $url = 'http://www.mysite.com';
    $ch = curl_init($url);
    
    // Tell curl_exec to return the text instead of sending it to STDOUT
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    // Don't include return header in output
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    $txt = curl_exec($ch);
    curl_close($ch);
    
    echo $txt;
    

    curl通常是一个更好的选择,因为您可以更好地控制确切的请求参数,并在事情没有按计划进行时更多地进行错误处理。

    如果远程API页面返回JSON字符串,则使用json_decode()将其转换为数组,并迭代生成的数组以提取所需的值:

    // retrieve the remote text using one of the above methods
    $txt = file_get_contents('http://www.example.com/');
    
    // decode the json string into an array
    $arr = json_decode($txt);
    
    $price_arr = array();
    
    foreach ($arr as $a) {
      // this is not the exact format of the data returned
      // from the API, just an example to demonstrate how you
      // would iterate over the decoded array
      $price_arr[] = $a['price'];
    }
    

    你也可以使用PHP的array_map()函数来更详细地解析因解码JSON字符串而产生的数组,尽管一个简单的foreach循环足以满足大多数人的需要。