dragon8899
dragon8899
2011-12-10 17:16

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

已采纳

I'm running this in the URL:

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

This gives me output like:

{
 "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"
     }
    ]
   }
  }

I want to get all the values from price and store them into an array in php? How do I run this query on somepage.php and output the json values into a php array within that page?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongtaijue1578 dongtaijue1578 10年前

    The solution to this problem, in steps:

    1. Retrieve the remote source text
    2. Decode the JSON string into an array
    3. Iterate over the resulting array to retrieve your desired data.

    There are multiple ways to retrieve remote files/text in php. The most prominent are ...

    Using file_get_contents():

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

    And PHP's curl functions:

    $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 is generally a better option because you more control over the exact request parameters and more possibilities for error handling when things don't go as planned.

    If the remote API page returns a JSON string, you turn it into an array using json_decode() and iterate the resulting array to pull out the values you want:

    // 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'];
    }
    

    You could also use PHP's array_map() function for more detailed parsing of the array that results from decoding the JSON string, though a simple foreach loop will suffice for most people.

    点赞 评论 复制链接分享