내 사이트에 통합하려고하는 소포 추적 서비스의 피드가 있습니다. 끝에 추적 번호를 넣고 json 응답을받을 수있는 URL이 있습니다. 나는 보낸 사람 주소 및 추적 진행 상황과 같이 foreach를 사용해야하는 정보와 같은 정적 정보를 포함하는 여러 개체가 있습니다.URL에서 PHP를 사용하여 JSON 응답 표시
나는 문자열을 알았지 만 정보를 표시하는 방법을 모르겠다.
예 URL :
domain.com/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665
이
내가 지금까지 무엇을 가지고 URL 반환 :{
"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
}
현재 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;
난 여전히 오류를 얻고 당신의 예에 따라 접속하시면. 알림 : 29 행의 parcel-tracking.php에있는 non-object의 속성을 얻으려고 시도 했습니까? –
문자열이 예제에서 추가 한 것과 다를 수 있습니다. 내 대답에 정확한 코드를 실행하면 작동합니다 (여기를 확인하십시오 : http://sandbox.onlinephpfunctions.com/code/cab3a789c3dfaa70e11e8032e3b37bd3c91df64b) – Dekel
이것은 내 전체 코드입니다. http://sandbox.onlinephpfunctions.com/code/8b97b4a00708d7ddbfa1c0352ddd1e3d1829a997 URL로 이동하면 출력이 –