我不是很好使用PHP和cURL,通常我只是用javascript或C#打電話,但是我正在使用wordpress,因此C#不可能,而且我在調用url中有一個apikey,所以我想知道我是否可以得到一些幫助。在JavaScript中,電話會。PHP curl headers
var forecastOptions = {
"cache": false,
"dataType": "jsonp",
"url": callSite
};
var forecastRequest = $.ajax(forecastOptions);
我這樣做是爲了我的可讀性。我也不想打開「allow_url_fopen選項」
編輯
因此,這是我現在有。
<?php
$api_key = 'xxxxxxxxxx';
$latitude = "40.5122";
$longitude = "-88.9886";
$API_ENDPOINT = 'https://api.forecast.io/forecast/';
$request_url = $API_ENDPOINT .
$api_key . '/' .
$latitude . ',' . $longitude;
$ch = curl_init();
$headers = array(
'Content-Type: application/json',
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, $headers);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$content = json_decode($result, true);
if(empty($content)){
print_r("Empty");
}
?>
這是告訴我,$內容是空的。如果有什麼,我錯過了什麼。
我已經更新了我的代碼,但我沒有收到任何請求。我在代碼中丟失了什麼嗎?我正試圖從forecast.io api中提取。 – pormus 2014-12-03 18:22:24
你應該嘗試從瀏覽器中使用的URL,看它是否返回任何東西。 您還應該從上面的$ httpCode中檢查HTTP狀態。 – 2014-12-03 19:26:20
我做了print_r($ httpCode),它給了我0.我接通了網址,並把它放在瀏覽器的網址中,以確保它是正確的網址(它是)然後我拿了並粘貼該網址代替$ request_url。我仍然得到相同的結果。 – pormus 2014-12-03 19:33:14