0

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": "[email protected]",
    "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 Answers 2

1

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": "[email protected]",
    "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
Sign up to request clarification or add additional context in comments.

7 Comments

When accessing it as per your example I am still getting an error. Notice: Trying to get property of non-object in parcel-tracking.php on line 29
Maybe your string is not what you added in the example? If you run the exact code in my answer it works (check here: sandbox.onlinephpfunctions.com/code/…)
This is my full code: sandbox.onlinephpfunctions.com/code/… If you go to the URL you will see the output
The problem is with the URL. Did you check it (in your browser)? :)
Please double check. Copy & Paste the exact URL from the code you posted :)
|
0

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": "[email protected]",
    "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:\n</br>";
    } else {
        echo "$key => $val\n";
    }
}

OUTPUT:

    Agent => Consignee:
Address1 => 25 HEATHFIELD ROAD Address2 => SHOLING Address3 => Code => Company => ERIK HANSON Country => GREAT BRITAIN Dept => Email => [email protected] 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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.