Getting specific value from JSON in php from UPCitemdb API -


this question has answer here:

i trying 1 attribute-value pair called title.

this sample code website , returns whole json data. how return title , store string variable in php?

 <<?php $user_key = 'only_for_dev_or_pro'; $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); curl_setopt($ch, curlopt_httpheader, array(  ));  // http 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."\n"; /* if need run more queries, them in same connection. * use rawurlencode() instead of urlencode(), if set search string  * url query param  */ sleep(2); // proceed other queries curl_close($ch); 

json:

http/1.1 200 ok server: openresty/1.9.7.4 date: fri, 14 apr 2017 14:33:27 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: 1492263082 x-ratelimit-remaining: 87 etag: w/"baa-nup5zq4a0espwokpnkuzvg" 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 great newer cooks or use in second home. these value priced knives quite sharp. high-carbon stainless steel, sharp edge long lasting. triple riveted handles extremely durable. hand wash only. lifetime warranty wusthof normal use , 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=1492180407&plt=8e28d8273c644252688700da99d3ae40","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=1492180407&plt=353d82b7403f611d0a99d3a14c1aeba2","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=1492180407&plt=47ce957e7b574db04285b40e11ed5816","updated_t":1425619323},{"merchant":"rakuten(buy.com)","domain":"rakuten.com","title":"wusthof gourmet - 3 serrated utility knife","currency":"","list_price":"","price":19.95,"shipping":"8.95","condition":"new","availability":"","link":"http://www.upcitemdb.com/norob/alink/?id=w2v233w21303c444&tid=1&seq=1492180407&plt=57e9f7ffdcf388e9c3c4e81403c0ddc9","updated_t":1476903025},{"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=1492180407&plt=b3a67678a759c8e8d949c4e7b26d0207","updated_t":1448989675}],"asin":"b0000djye3","elid":"192088094351"}]}

try , should use curl_setopt($ch, curlopt_header, false); prevent returning headers in curl response.

<?php ini_set('display_errors', 1); $user_key = 'only_for_dev_or_pro'; $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); curl_setopt($ch, curlopt_header, false); curl_setopt($ch, curlopt_httpheader, array(  ));  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."\n";  /* if need run more queries, them in same connection. * use rawurlencode() instead of urlencode(), if set search string  * url query param  */ sleep(2); // proceed other queries curl_close($ch);  $result=json_decode($response,true); echo $title=$result['items'][0]["title"]; 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -