dongyiyu3953 2016-10-20 10:26
浏览 42
已采纳

使用URL中的PHP显示JSON响应

I have a feed from a parcel tracking service that i am trying to integrate into my site. I have a url which allows me to put the tracking number at the end and get a json response. I have multiple objects from this which include some static info such as the senders address and some info which I will need to use a foreach for like the tracking progress.

I believe I have got the string okay however I am not sure how I am meant to display the info.

This is what I have so far:

Example URL:

domain.com/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665

URL Returns:

{
  "Agent": null,
  "Consignee": {
    "Address1": "25 HEATHFIELD ROAD",
    "Address2": "SHOLING",
    "Address3": "",
    "Code": null,
    "Company": "ERIK HANSON",
    "Country": "GREAT BRITAIN",
    "Dept": "",
    "Email": "ERIK.HANSON66@GOOGLEMAIL.COM",
    "Name": "",
    "Phone": "07770320490",
    "Postcode": "SO19 1DL",
    "State": "HANTS",
    "Town": "SOUTHAMPTON"
  },
  "CrossIdx": "",
  "Error": null,
  "NonDel": null,
  "POD": {
    "Date": "13 Jul 2016",
    "Status": "Harnett",
    "Time": "09:17"
  },
  "Pieces": 1,
  "PosErr": 0,
  "Tracks": [
    {
      "Date": "13 Jul 2016",
      "Status": "Out for delivery",
      "Time": "07:10"
    },
    {
      "Date": "13 Jul 2016",
      "Status": "At Delivery location Portsmouth",
      "Time": "02:24"
    },
    {
      "Date": "13 Jul 2016",
      "Status": "At Delivery location Portsmouth",
      "Time": "02:22"
    },
    {
      "Date": "12 Jul 2016",
      "Status": "Arrived At Ryton",
      "Time": "22:12"
    },
    {
      "Date": "12 Jul 2016",
      "Status": "Preparing for despatch",
      "Time": "14:00"
    },
    {
      "Date": "12 Jul 2016",
      "Status": "Scanned into OCS HEATHROW LONDON",
      "Time": "13:59"
    },
    {
      "Date": "12 Jul 2016",
      "Status": "Consignment details verified",
      "Time": "13:59"
    },
    {
      "Date": "14 Jun 2016",
      "Status": "Shipment prepared by customer",
      "Time": "11:20"
    },
    {
      "Date": "02 Jan 2003",
      "Status": "Collected from Customer",
      "Time": "09:32"
    }
  ],
  "Weight": 7
}

Current PHP:

//set tracking url
$url = "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665";

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

//call api
//$json = file_get_contents($url);
$json = json_decode($output);
$Address1 = $json->results[0]->Consignee->Address1;
$Address2 = $json->results[0]->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
  • 写回答

2条回答 默认 最新

  • dsfhe34889 2016-10-20 10:35
    关注

    Your json string doesn't have any results key (so I'm not sure why you trying to access results[0].

    You can just use

    $Address1 = $json->Consignee->Address1;
    $Address2 = $json->Consignee->Address2;
    

    Check this code:

    $s = <<< END
    {
      "Agent": null,
      "Consignee": {
        "Address1": "25 HEATHFIELD ROAD",
        "Address2": "SHOLING",
        "Address3": "",
        "Code": null,
        "Company": "ERIK HANSON",
        "Country": "GREAT BRITAIN",
        "Dept": "",
        "Email": "ERIK.HANSON66@GOOGLEMAIL.COM",
        "Name": "",
        "Phone": "07770320490",
        "Postcode": "SO19 1DL",
        "State": "HANTS",
        "Town": "SOUTHAMPTON"
      },
      "CrossIdx": "",
      "Error": null,
      "NonDel": null,
      "POD": {
        "Date": "13 Jul 2016",
        "Status": "Harnett",
        "Time": "09:17"
      },
      "Pieces": 1,
      "PosErr": 0,
      "Tracks": [
        {
          "Date": "13 Jul 2016",
          "Status": "Out for delivery",
          "Time": "07:10"
        },
        {
          "Date": "13 Jul 2016",
          "Status": "At Delivery location Portsmouth",
          "Time": "02:24"
        },
        {
          "Date": "13 Jul 2016",
          "Status": "At Delivery location Portsmouth",
          "Time": "02:22"
        },
        {
          "Date": "12 Jul 2016",
          "Status": "Arrived At Ryton",
          "Time": "22:12"
        },
        {
          "Date": "12 Jul 2016",
          "Status": "Preparing for despatch",
          "Time": "14:00"
        },
        {
          "Date": "12 Jul 2016",
          "Status": "Scanned into OCS HEATHROW LONDON",
          "Time": "13:59"
        },
        {
          "Date": "12 Jul 2016",
          "Status": "Consignment details verified",
          "Time": "13:59"
        },
        {
          "Date": "14 Jun 2016",
          "Status": "Shipment prepared by customer",
          "Time": "11:20"
        },
        {
          "Date": "02 Jan 2003",
          "Status": "Collected from Customer",
          "Time": "09:32"
        }
      ],
      "Weight": 7
    }
    END;
    
    $json = json_decode($s);
    $Address1 = $json->Consignee->Address1;
    $Address2 = $json->Consignee->Address2;
    echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
    

    Here is the output:

    Address 1: 25 HEATHFIELD ROAD, Address 2: SHOLING
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器