dongyiyu3953
dongyiyu3953
2016-10-20 10:26

使用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 dsfhe34889 5年前

    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
    
    点赞 评论 复制链接分享
  • duanji1482 duanji1482 5年前

    Below is json string in $json variable and access with RecursiveIteratorIterator

    Example

    $json='{
      "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
    }';
     $jsonIterator = new RecursiveIteratorIterator(
     new RecursiveArrayIterator(json_decode($json, TRUE)),
     RecursiveIteratorIterator::SELF_FIRST);
    
    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {
            echo "$key:
    </br>";
        } else {
            echo "$key => $val
    ";
        }
    }
    

    OUTPUT:

        Agent => Consignee:
    Address1 => 25 HEATHFIELD ROAD Address2 => SHOLING Address3 => Code => Company => ERIK HANSON Country => GREAT BRITAIN Dept => Email => ERIK.HANSON66@GOOGLEMAIL.COM Name => Phone => 07770320490 Postcode => SO19 1DL State => HANTS Town => SOUTHAMPTON CrossIdx => Error => NonDel => POD:
    Date => 13 Jul 2016 Status => Harnett Time => 09:17 Pieces => 1 PosErr => 0 Tracks:
    0:
    Date => 13 Jul 2016 Status => Out for delivery Time => 07:10 1:
    Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:24 2:
    Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:22 3:
    Date => 12 Jul 2016 Status => Arrived At Ryton Time => 22:12 4:
    Date => 12 Jul 2016 Status => Preparing for despatch Time => 14:00 5:
    Date => 12 Jul 2016 Status => Scanned into OCS HEATHROW LONDON Time => 13:59 6:
    Date => 12 Jul 2016 Status => Consignment details verified Time => 13:59 7:
    Date => 14 Jun 2016 Status => Shipment prepared by customer Time => 11:20 8:
    Date => 02 Jan 2003 Status => Collected from Customer Time => 09:32 Weight => 7 
    
    点赞 评论 复制链接分享

相关推荐