How to parse SOAP response with PHP?

How do I parse JSON with PHP?

  • I am trying to get data from an external website using an api. I am trying to use simplexml and am running into a few problems. Edit and Update: So Everyone has told me to try json instead of xml. I switched to json and I have this so far: $response = file_get_contents($uri); //parse xml $myBeer = json_decode($response); how do I actually pick out the data from the json now? My json looks like this: { "currentPage" : 1, "status" : "success", "numberOfPages" : 1, "data" : [ { "status" : "verified", "breweries" : [ { "locations" : [ { "website" : "http://www…", "status" : "verified", "isPrimary" : "Y", "locality" : "Milton", "createDate" : "2012-01-03 02:41:53", "yearOpened" : "1995", "latitude" : 38.770622, "id" : "Mq24sa", "locationTypeDisplay" : "Micro Brewery", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "micro", "longitude" : -75.310893, "updateDate" : "2012-03-21 20:05:25", "inPlanning" : "N", "openToPublic" : "Y", "name" : "Main Brewery", "region" : "Delaware", "phone" : "302-684-1000", "streetAddress" : "6 Cannery Village Center", "countryIsoCode" : "US", "postalCode" : "19968" }, { "hoursOfOperation" : "Open 7 Days, …", "website" : "http://www…", "status" : "verified", "isPrimary" : "N", "locality" : "Rehoboth Beach", "createDate" : "2012-01-24 14:58:17", "yearOpened" : "1995", "latitude" : 38.715362, "id" : "57kBD4", "locationTypeDisplay" : "Brewpub", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "brewpub", "longitude" : -75.086898, "updateDate" : "2012-03-21 20:05:25", "inPlanning" : "N", "openToPublic" : "Y", "name" : "Dogfish Head Brewings & Eats", "region" : "Delaware", "phone" : "302-226-2739", "countryIsoCode" : "US", "streetAddress" : "320 Rehoboth Avenue", "postalCode" : "19971" }, { "hoursOfOperation" : "Monday…", "website" : "http://www…", "status" : "verified", "isPrimary" : "N", "locality" : "Gaithersburg", "createDate" : "2012-01-24 15:01:34", "yearOpened" : "1995", "latitude" : 39.142971, "id" : "hRSzMV", "locationTypeDisplay" : "Restaurant/Ale House", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "restaurant", "longitude" : -77.215924, "updateDate" : "2012-03-21 20:05:25", "inPlanning" : "N", "openToPublic" : "Y", "name" : "DFH Alehouse, Gaithersburg", "region" : "Maryland", "phone" : "301-963-4847", "countryIsoCode" : "US", "streetAddress" : "800 West Diamond Ave.", "postalCode" : "20878" }, { "hoursOfOperation" : "Monday…", "website" : "http://www…", "status" : "verified", "isPrimary" : "N", "locality" : "Falls Church", "createDate" : "2012-01-24 15:03:04", "yearOpened" : "1995", "latitude" : 38.870041, "id" : "JlBnVw", "locationTypeDisplay" : "Restaurant/Ale House", "statusDisplay" : "Verified", "extendedAddress" : "Seven Corners Shopping Center", "locationType" : "restaurant", "isClosed" : "N", "longitude" : -77.151446, "updateDate" : "2012-03-21 20:05:25", "inPlanning" : "N", "openToPublic" : "Y", "name" : "DFH Alehouse, Falls Church", "region" : "Virginia", "phone" : "703-534-3342", "countryIsoCode" : "US", "streetAddress" : "6220 Leesburg Pike", "postalCode" : "22044" }, { "hoursOfOperation" : "Monday…", "website" : "http://www…", "status" : "verified", "isPrimary" : "N", "locality" : "Fairfax", "createDate" : "2012-01-24 15:04:07", "yearOpened" : "1995", "latitude" : 38.880695, "id" : "w4ubfN", "locationTypeDisplay" : "Restaurant/Ale House", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "restaurant", "longitude" : -77.397333, "updateDate" : "2012-03-21 20:05:25", "inPlanning" : "N", "openToPublic" : "Y", "name" : "DFH Alehouse, Fairfax", "region" : "Virginia", "phone" : "703-961-1140", "countryIsoCode" : "US", "streetAddress" : "13041 Lee Jackson Memorial Hwy", "postalCode" : "22033" } ], "images" : { "large" : "http://s3.…", "icon" : "http://s3.…", "medium" : "http://s3.…" }, "website" : "http://www…", "updateDate" : "2012-04-23 23:50:56", "status" : "verified", "established" : "1995", "name" : "Dogfish Head Craft Brewery", "description" : "The story of…", "isOrganic" : "N", "createDate" : "2012-01-03 02:41:53", "id" : "g0jHqt", "statusDisplay" : "Verified" } ], "glasswareId" : 6, "createDate" : "2012-01-03 02:43:52", "id" : "YDBgUE", "labels" : { "large" : "http://s3.…", "icon" : "http://s3.…", "medium" : "http://s3.…" }, "available" : { "name" : "Limited", "id" : "2", "description" : "Limited availability." }, "statusDisplay" : "Verified", "styleId" : 34, "abv" : "15", "ibu" : "85", "updateDate" : "2012-03-22 13:04:36", "style" : { "categoryId" : 3, "ogMin" : "1.09", "ibuMin" : "60", "name" : "American-Style Barley Wine Ale", "description" : "American…", "abvMax" : "12", "abvMin" : "8.4", "createDate" : "2012-03-21 20:06:46", "srmMax" : "22", "srmMin" : "11", "fgMin" : "1.024", "fgMax" : "1.028", "id" : 34, "category" : { "createDate" : "2012-03-21 20:06:45", "name" : "North American Origin Ales", "id" : 3 }, "ibuMax" : "100" }, "name" : "Olde School Barleywine", "description" : "Bold…", "isOrganic" : "N", "type" : "beer", "availableId" : 2, "glass" : { "createDate" : "2012-01-03 02:41:33", "name" : "Snifter", "id" : 6 } }, { "status" : "verified", "breweries" : [ { "locations" : [ { "website" : "http://www…", "status" : "verified", "isPrimary" : "Y", "locality" : "Springfield", "createDate" : "2012-01-03 02:42:02", "yearOpened" : "2011", "latitude" : 37.207508, "id" : "QGZzrv", "locationTypeDisplay" : "Micro Brewery", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "micro", "longitude" : -93.299385, "updateDate" : "2012-03-21 20:05:27", "inPlanning" : "N", "openToPublic" : "Y", "name" : "Main Brewery", "region" : "Missouri", "phone" : "417-862-0423", "streetAddress" : "215 S Grant Avenue", "countryIsoCode" : "US", "postalCode" : "65806" } ], "images" : { "large" : "http://s3.…", "icon" : "http://s3.…", "medium" : "http://s3.…" }, "website" : "http://www…", "updateDate" : "2012-03-21 19:06:11", "status" : "verified", "established" : "2011", "name" : "Mother's Brewing Company", "description" : "Mother's Brewing…", "isOrganic" : "N", "createDate" : "2012-01-03 02:42:02", "id" : "Xknbgp", "statusDisplay" : "Verified" } ], "glasswareId" : 5, "createDate" : "2012-01-03 02:43:51", "id" : "j8BSpW", "labels" : { "large" : "http://s3.…", "icon" : "http://s3.…", "medium" : "http://s3.…" }, "available" : { "name" : "Seasonal", "id" : "4", "description" : "Available at the same time of year, every year." }, "styleId" : 81, "statusDisplay" : "Verified", "abv" : "5.5", "updateDate" : "2012-03-22 13:05:23", "name" : "Old School", "style" : { "categoryId" : 7, "ogMin" : "1.05", "ibuMin" : "18", "name" : "German-Style Märzen", "description" : "Märzens are characterized by a medium…", "abvMax" : "5.9", "abvMin" : "5.3", "createDate" : "2012-03-21 20:06:46", "srmMax" : "15", "srmMin" : "4", "fgMin" : "1.012", "fgMax" : "1.02", "id" : 81, "category" : { "createDate" : "2012-03-21 20:06:46", "name" : "European-germanic Lager", "id" : 7 }, "ibuMax" : "25" }, "isOrganic" : "N", "type" : "beer", "availableId" : 4, "glass" : { "createDate" : "2012-01-03 02:41:33", "name" : "Pint", "id" : 5 } }, { "status" : "verified", "breweries" : [ { "locations" : [ { "hoursOfOperation" : "Open 7 Days a week 11am until Midnight", "website" : "http://www…", "status" : "verified", "isPrimary" : "Y", "locality" : "Asheville", "createDate" : "2012-01-03 02:41:44", "yearOpened" : "1995", "latitude" : 35.622131, "id" : "JDYdnx", "locationTypeDisplay" : "Brewpub", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "brewpub", "longitude" : -82.553603, "updateDate" : "2012-03-21 20:05:24", "inPlanning" : "N", "openToPublic" : "Y", "name" : "North Asheville", "region" : "North Carolina", "phone" : "828-254-1281", "countryIsoCode" : "US", "streetAddress" : "675 Merrimon Avenue", "postalCode" : "28804" }, { "hoursOfOperation" : "Monday…", "website" : "http://ash…", "status" : "verified", "isPrimary" : "N", "locality" : "Asheville", "createDate" : "2012-01-03 19:29:20", "yearOpened" : "1995", "latitude" : 35.592393, "id" : "IwcIL8", "locationTypeDisplay" : "Brewpub", "statusDisplay" : "Verified", "isClosed" : "N", "locationType" : "brewpub", "longitude" : -82.554997, "updateDate" : "2012-03-21 20:05:24", "inPlanning" : "N", "openToPublic" : "Y", "name" : "Downtown Asheville", "region" : "North Carolina", "phone" : "828-255-4077", "countryIsoCode" : "US", "streetAddress" : "77 Coxe Avenue", "postalCode" : "28804" } ], "images" : { "large" : "http://s3.…", "icon" : "http://s3.…", "medium" : "http://s3.…" }, "website" : "http://www…", "updateDate" : "2012-03-21 19:06:03", "status" : "verified", "established" : "1995", "name" : "Asheville Brewing Company", "description" : "A gem to the Asheville…", "isOrganic" : "N", "createDate" : "2012-01-03 02:41:44", "id" : "WDrFlD", "statusDisplay" : "Verified" } ], "srm" : { "name" : "6", "hex" : "F8A600" }, "createDate" : "2012-01-03 23:13:24", "id" : "awJ2XZ", "available" : { "name" : "Year Round", "id" : "1", "description" : "Available year round as a staple beer." }, "statusDisplay" : "Verified", "styleId" : 5, "abv" : "3.2", "updateDate" : "2012-03-22 13:05:45", "srmId" : 6, "name" : "Old School", "style" : { "categoryId" : 1, "ogMin" : "1.046", "ibuMin" : "30", "name" : "Extra Special Bitter", "description" : "Extra special bitter…", "abvMax" : "5.8", "abvMin" : "4.8", "createDate" : "2012-03-21 20:06:45", "srmMax" : "14", "srmMin" : "8", "fgMin" : "1.01", "fgMax" : "1.016", "id" : 5, "category" : { "createDate" : "2012-03-21 20:06:45", "name" : "British Origin Ales", "id" : 1 }, "ibuMax" : "45" }, "description" : "Sometimes you've just…", "isOrganic" : "N", "type" : "beer", "availableId" : 1 } ] } Update: Whats the best way to handle pagination? So I can get all the results? I tried making a few for loops within each other but it did not work?

  • Answer:

    According to your JSON code, the following code should work: $response = file_get_contents($uri); $json = json_decode($response); foreach ($json->data as $beer) { echo $beer->name."\n"; } If you want to know how many beers there are, use count($json->data). With multiple pages, the code may be as follow: // here we store the beers names $beer_names = array(); // the current page $page = 0; // the maximum number of pages // (we don't know for the moment, so // we set it to 0) $max_pages = 0; do { $page++; // get the data for page $page $response = file_get_contents($uri.'&p='.$page); // decode the data $json = json_decode($response); foreach ($json->data as $beer) { $beer_names []= $beer->name: } // if $max_pages is 0 (i.e. is not set), we set // it with the given value if ($max_pages == 0) { $max_pages = $json->numberOfPages; } } while ($page < $max_pages); echo implode("\n", $beer_names);

Baptiste Fontaine at Quora Visit the source

Was this solution helpful to you?

Other answers

Do a var_dump on $myBeer. You will see that all parts wrapped in {} are not StdClass and all parts wrapped in [] are arrays. This means that to get the 'currentPage' you would call $myBeer->currentPage. To get the first element in the data array you would call $myBeer->data[0].

Lonny Kapelushnik

Sorry for the late answer. Try something like this foreach ( $myBeer->trends as $trend ) { echo "{$trend->name}\n"; } From (http://stackoverflow.com/questions/263392/handling-data-in-a-php-json-object)

Frankie Nwafili

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.