Forum Moderators: coopster

Message Too Old, No Replies

Process curl array results to usable php variables

         

cffrost2

3:38 pm on Jun 6, 2017 (gmt 0)

10+ Year Member



I'm using this PHP script to get data from a url using curl.


<?php
$endpoint = 'https://api.upcitemdb.com/prod/trial/lookup';

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

// HTTP GET
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $endpoint.'?upc=4002293401102');
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode != 200)
echo "error status $httpcode...\n";
else
echo $response;
/* if you need to run more queries, do them in the same connection.
* use rawurlencode() instead of URLEncode(), if you set search string
* as url query param
*/
sleep(2);
// proceed with other queries
curl_close($ch);

?>


How can I process the below results to usable variables. I need to cleanly output the items, images, and offers to html using PHP and I'm stumped. Any help would be much appreciated.


HTTP/1.1 200 OK
Server: openresty/1.9.7.4
Date: Tue, 06 Jun 2017 14:57:34 GMT
Content-Type: application/json;
charset=utf-8
Content-Length: 2986
Connection: keep-alive
X-Powered-By: Express
X-RateLimit-Limit: 100
X-RateLimit-Reset: 1496782300
X-RateLimit-Remaining: 58
ETag: W/"baa-BVo3BGeKmp0AnMo8J1wJzg"
Vary: Accept-Encoding
Access-Control-Allow-Origin: http://www.upcitemdb.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type,Accept
Access-Control-Expose-Headers: Content-Type,X-RateLimit-Remaining,X-RateLimit-Limit,X-RateLimit-Reset,total,offset
{
"code":"OK",
"total":1,
"offset":0,
"items":[
{
"ean":"4002293401102",
"title":"Wusthof Gourmet 3-Inch Serrated Paring Knife",
"description":"A Wusthof Gourmet stamped knife is great for newer cooks or for use in a second home. These value priced knives are quite sharp. Thanks to their high-carbon stainless steel, their sharp edge is long lasting. Triple riveted handles are extremely durable. Hand wash only. Lifetime warranty from Wusthof with normal use and proper care. Made in Solingen Germany.",
"brand":"W?sthof",
"model":"4011-7",
"color":"Multi",
"size":"",
"dimension":"8 X 1X 1 inches",
"weight":"0.1 Pounds",
"currency":"",
"lowest_recorded_price":12.99,
"images":[
"http://www.chefscatalog.com/img/products/500x500/99528_500.jpg",
"http://img1.r10.io/PIC/84707643/0/1/250/84707643.jpg",
"http://c.shld.net/rpx/i/s/pi/mp/33108/2456981411?src=http%3A%2F%2Fwww.chefscatalog.com%2Fimg%2Fproducts%2F1000x1000%2F99528_1000.jpg&d=8ba15ee3bfc8785ae78e62c3c0762bcb86214ed4",
"http://images10.newegg.com/ProductImageCompressAll200/A0NV_1_20120523_5340513.jpg",
"http://images.prosperentcdn.com/images/250x250/cdn.metrokitchen.com/images/uploads/wu-4011-zoomed.jpg"
],
"offers":[
{
"merchant":"MetroKitchen",
"domain":"metrokitchen.com",
"title":"Wusthof Gourmet 3 inch Serrated Paring Knife",
"currency":"",
"list_price":"",
"price":19.95,
"shipping":"",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=y2x203y2x26384&tid=1&seq=1496761054&plt=b5697879cd221535aad6e95eaaf9f6db",
"updated_t":1466567204},
{
"merchant":"Newegg.com",
"domain":"newegg.com",
"title":"Wusthof Gourmet - 3\" Serrated Utility Knife",
"currency":"",
"list_price":"",
"price":15.95,
"shipping":"5.95",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=v2t2z2u2v2x2c4a4&tid=1&seq=1496761054&plt=8eddebbd84f61019330190997bc18710",
"updated_t":1481154945
},
{
"merchant":"Sears",
"domain":"sears.com",
"title":"Wusthof Gourmet 3-in. Serrated Paring Knife",
"currency":"",
"list_price":"",
"price":19.95,
"shipping":"",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=u2p2631343x2a4d4x2&tid=1&seq=1496761054&plt=cf765b1568727577c5226c5efe2474a3",
"updated_t":1425619323
},
{
"merchant":"Rakuten(Buy.com)",
"domain":"rakuten.com",
"title":"Wusthof Gourmet - 3 Serrated Utility Knife",
"currency":"",
"list_price":"",
"price":16.95,
"shipping":"5.95",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=w2v233w21303c444&tid=1&seq=1496761054&plt=05b7eba2926539453d156d9b2534e1ca",
"updated_t":1496267872
},
{
"merchant":"Chefs Catalog",
"domain":"chefscatalog.com",
"title":"Wusthof Gourmet 3-in. Serrated Paring Knife - Wusthof Gourmet",
"currency":"",
"list_price":26,"price":15.95,
"shipping":"Free Shipping",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=u2o243z2w203b454s2&tid=1&seq=1496761054&plt=b09daf52d0180981bead3c50a6b56b45",
"updated_t":1448989675
}
],
"asin":"B0000DJYE3",
"elid":"282501593152"
}
]
}

Peter_S

3:54 pm on Jun 6, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



The header mentions it's "Content-Type: application/json; ". So you simply use the "json_decode" function of PHP, you pass the content (without the header so) and "true" as the second parameter : [php.net...]

cffrost2

4:18 pm on Jun 6, 2017 (gmt 0)

10+ Year Member



Hey Peter_S.
I found that also. I tried "json_decode($response, true)" but still can not figure out how to access each variable in the nested arrays to use them as needed. Some research showed to use a foreach loop (foreach($results as $key => $value) { echo $value->items }) but I got an error "NOTICE: invalid foreach argument" And also got an error trying to access a non object value. And I believe the "true" processes the output as an object.

I can't seem to wrap my mind around this and how I can make it usable the way I need it.

And in the header you'll see "X-RateLimit-Remaining: 58". I can only send 100 requests a day. I have 58 tries left for today. LOL :)

Peter_S

4:28 pm on Jun 6, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



First of all, instead of querying the distance server, for each of your tries, simply save the answer into a text file, then you work with the content of the text file, to experiment. In the example bellow, I put it into a variable.

<?php
$content = '
{
"code":"OK",
"total":1,
"offset":0,
"items":[
{
"ean":"4002293401102",
"title":"Wusthof Gourmet 3-Inch Serrated Paring Knife",
"description":"A Wusthof Gourmet stamped knife is great for newer cooks or for use in a second home. These value priced knives are quite sharp. Thanks to their high-carbon stainless steel, their sharp edge is long lasting. Triple riveted handles are extremely durable. Hand wash only. Lifetime warranty from Wusthof with normal use and proper care. Made in Solingen Germany.",
"brand":"W?sthof",
"model":"4011-7",
"color":"Multi",
"size":"",
"dimension":"8 X 1X 1 inches",
"weight":"0.1 Pounds",
"currency":"",
"lowest_recorded_price":12.99,
"images":[
"http://www.chefscatalog.com/img/products/500x500/99528_500.jpg",
"http://img1.r10.io/PIC/84707643/0/1/250/84707643.jpg",
"http://c.shld.net/rpx/i/s/pi/mp/33108/2456981411?src=http%3A%2F%2Fwww.chefscatalog.com%2Fimg%2Fproducts%2F1000x1000%2F99528_1000.jpg&d=8ba15ee3bfc8785ae78e62c3c0762bcb86214ed4",
"http://images10.newegg.com/ProductImageCompressAll200/A0NV_1_20120523_5340513.jpg",
"http://images.prosperentcdn.com/images/250x250/cdn.metrokitchen.com/images/uploads/wu-4011-zoomed.jpg"
],
"offers":[
{
"merchant":"MetroKitchen",
"domain":"metrokitchen.com",
"title":"Wusthof Gourmet 3 inch Serrated Paring Knife",
"currency":"",
"list_price":"",
"price":19.95,
"shipping":"",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=y2x203y2x26384&tid=1&seq=1496761054&plt=b5697879cd221535aad6e95eaaf9f6db",
"updated_t":1466567204},
{
"merchant":"Newegg.com",
"domain":"newegg.com",
"title":"Wusthof Gourmet - 3\" Serrated Utility Knife",
"currency":"",
"list_price":"",
"price":15.95,
"shipping":"5.95",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=v2t2z2u2v2x2c4a4&tid=1&seq=1496761054&plt=8eddebbd84f61019330190997bc18710",
"updated_t":1481154945
},
{
"merchant":"Sears",
"domain":"sears.com",
"title":"Wusthof Gourmet 3-in. Serrated Paring Knife",
"currency":"",
"list_price":"",
"price":19.95,
"shipping":"",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=u2p2631343x2a4d4x2&tid=1&seq=1496761054&plt=cf765b1568727577c5226c5efe2474a3",
"updated_t":1425619323
},
{
"merchant":"Rakuten(Buy.com)",
"domain":"rakuten.com",
"title":"Wusthof Gourmet - 3 Serrated Utility Knife",
"currency":"",
"list_price":"",
"price":16.95,
"shipping":"5.95",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=w2v233w21303c444&tid=1&seq=1496761054&plt=05b7eba2926539453d156d9b2534e1ca",
"updated_t":1496267872
},
{
"merchant":"Chefs Catalog",
"domain":"chefscatalog.com",
"title":"Wusthof Gourmet 3-in. Serrated Paring Knife - Wusthof Gourmet",
"currency":"",
"list_price":26,"price":15.95,
"shipping":"Free Shipping",
"condition":"New",
"availability":"",
"link":"http://www.upcitemdb.com/norob/alink/?id=u2o243z2w203b454s2&tid=1&seq=1496761054&plt=b09daf52d0180981bead3c50a6b56b45",
"updated_t":1448989675
}
],
"asin":"B0000DJYE3",
"elid":"282501593152"
}
]
}' ;

$arr = json_decode ( $content , true ) ;

$items = $arr [ 'items' ] ;
$items_count = count ( $items ) ;


now, you have your items into the $items variable. I am not sure if this is it that you are trying to achieve.

cffrost2

9:37 pm on Jun 7, 2017 (gmt 0)

10+ Year Member



Ok. I've been able to sort out the $content and drilling it down as I need. Now, when I fetch the data from the remote server, it returns the header info as shown and the body which is what I've been able to sort out which then breaks because of the included headers. I do need to access some of the header info so I can't not include it. So how do I do this with this header info in the result as well?

Peter_S

9:51 pm on Jun 7, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



$headers = curl_getinfo ( $ch ) ;

You'll get all header fields as an associative array : [php.net...]

$content = substr ( $response , $headers [ 'header_size' ] ) ;

This should extract the content from the whole response.