doujimiao7480 2017-04-14 14:31
浏览 58
已采纳

从UPCitemdb API获取PHP中JSON的特定值[重复]

This question already has an answer here:

I am trying to only one attribute-value pair called title.

This is a sample code from the website and it returns the whole JSON data. How do I get it to just return title and then store it as a 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 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...
";
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);

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

</div>
  • 写回答

2条回答 默认 最新

  • drlhsfqoa350437979 2017-04-14 14:43
    关注

    Try this and you should use curl_setopt($ch, CURLOPT_HEADER, false); to 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...
    ";
    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);
    
    $result=json_decode($response,true);
    echo $title=$result['items'][0]["title"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog