2017-10-03 2 views
-2

PHP를 사용하여이 API의 출력을 얻으려고합니다. 호주 우체국 운임 계산기입니다. 나는 그것이 무엇이 잘못되었는지 확신하지 못한다. 정말 도움이 될 것입니다.PHP에서 API 출력을 얻으려고 시도합니다.

// Set your API key: remember to change this to your live API key in production 
$apiKey = API_KEY; 

// Set the URL for the Domestic Parcel Size service 
$urlPrefix = URL_PREFIX; 
$parcelTypesURL = 'https://' . $urlPrefix . '/postage/parcel/domestic/size.json'; 

// Lookup domestic parcel types (different kinds of standard boxes etc) 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $parcelTypesURL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AUTH-KEY: ' . $apiKey)); 
$rawBody = curl_exec($ch); 

// Check the response: if the body is empty then an error occurred 
if(!$rawBody){ 
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch)); 
} 

// All good, lets parse the response into a JSON object 
$parcelTypesJSON = json_decode($rawBody); 
+0

API 키인 경우이를 숨겨야합니다. 이 코드가 오류를 발생시키는 것입니까? – Luke

+0

그 코드가 작동합니다. 'print_r ($ ParcelTypesJSON)'은 예상했던 데이터를 보여줍니다. 오류가 있으면 해당 코드 외부에 있습니다. – Luke

+0

경고 번호 9의 보안상의 이유로 curl_init()가 비활성화되었습니다. 이것은 경고 중 하나이며이 키는 숨기지 않은 테스트 용입니다 – ovic

답변

1

curl_init()는

이 서버가 해당 기능을 사용할 수 없습니다 것을 의미합니다 ... 보안상의 이유로 사용할 수 없습니다.
서버를 제어 할 수있는 경우 에서 curl_init()을 사용하도록 설정하십시오. More information here.
그렇지 않은 경우 file_get_contents()을 사용해보세요. more information here

+1

또는 - 호스트가 말려 올리지 못하고 말려 야하는 경우 호스트 이동 – rtfm

관련 문제