2017-04-14 58 views
-2

我想只有一個稱爲title的屬性 - 值對。從UPCitemdb在PHP中獲取JSON的具體值API

這是一個來自網站的示例代碼,它返回整個JSON數據。我如何得到它只是返回標題,然後將它作爲一個字符串變量存儲在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 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."\n"; 
/* 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); 

JSON:

HTTP/1.1 200 OK服務器:openresty/1.9.7.4日期:星期五,2017年4月14日14時33分27秒GMT內容類型:應用程序/ JSON的; X-RateLimit-Limit:100 X-RateLimit-Reset:1492263082 X-RateLimit-Remaining:87Endag: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英寸鋸齒削皮刀」,「描述」:「Wusthof美食加蓋的刀子適用於較新的廚師或在第二個家中使用,價格合理,價格合理,價格合理,價格合理,價格合理,價格合理,價格合理,歡迎來電諮詢。 , 「品牌」:「W S'顏色「:」多「,」尺寸「:」「,」尺寸「:」8 X 1X 1英寸「,」重量「:」0.1磅「,」貨幣「:」「,」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 「,」域名「:」metrokitchen.com「,」title「:」Wusthof Gourmet 3英寸鋸齒削皮刀「,」貨幣「:」「,」list_price「:」「,」價格「:19.95,」shipping「: 「」,「條件」:「新」,「可用性」:「」,「鏈接」:「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「鋸齒效用刀」,「貨幣」:「」,「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「 :「Wusthof Gourmet 3-in。 Serrated Paring Knife「,」currency「:」「,」list_price「:」「,」price「:19.95,」shipping「:」「,」condition「:」New「,」availability「:」「, :「http://www.upcitemdb.com/norob/alink/?id=u2p2631343x2a4d4x2&tid=1&seq=1492180407&plt=47ce957e7b574db04285b40e11ed5816」,「updated_t」:1425619323},{「merchant」:「Rakuten(Buy.com)」,「域名」:「rakuten.com」,「標題」:「Wusthof Gourmet - 3鋸齒效用刀」, 「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」:「廚師目錄」,「域名」:「chefscatalog.com」,「標題」:「Wusthof Gourmet 3-in。鋸齒削皮刀 - Wusthof Gourmet「,」貨幣「:」「,」list_price「:26,」價格「:15.95,」shipping「:」免運費「,」條件「:」新「,」可用性「:」 , 「鏈接」: 「http://www.upcitemdb.com/norob/alink/?id=u2o243z2w203b454s2&tid=1&seq=1492180407&plt=b3a67678a759c8e8d949c4e7b26d0207」, 「updated_t」:1448989675}], 「ASIN」: 「B0000DJYE3」, 「ELID」: 「192088094351」}]}

+0

你能分享你的'json'嗎? –

+0

@SahilGulati更新 – thenoob

+0

沒有。與名稱標題的鍵。你要哪一個?爲項目或提供? –

回答

2

試試這個,你應該使用curl_setopt($ch, CURLOPT_HEADER, false);來防止在curl響應中返回頭文件。

<?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 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); 

$result=json_decode($response,true); 
echo $title=$result['items'][0]["title"]; 
+0

沒有打印 – thenoob

+0

@thenoob您是否嘗試過我的帖子 –

+0

的演示版,但您已將其手動存儲到$ response中,所以也許這就是爲什麼它可以在演示中使用? – thenoob

1

您正在尋找json_decode

json_decode創建具有JSON字符串

$ OBJ = json_decode($響應)的對象;

$示例= $ obj-> yourAttribute;

foreach($ obj-> items as $ item) $ title = $ item-> title;

+0

如何讓它顯示一個稱爲標題的屬性值對而不是整個東西? – thenoob