PHP卷曲头
我不太擅长使用PHP和cURL,通常我只是用javascript或C打电话,但是我使用wordpress,所以C不可能,我在电话url中有一个apikey,所以我想知道我是否可以在这方面得到一些帮助。在javascript中,调用将是PHP卷曲头,php,json,curl,jsonp,Php,Json,Curl,Jsonp,我不太擅长使用PHP和cURL,通常我只是用javascript或C打电话,但是我使用wordpress,所以C不可能,我在电话url中有一个apikey,所以我想知道我是否可以在这方面得到一些帮助。在javascript中,调用将是 var forecastOptions = { "cache": false, "dataType": "jsonp", "url": callSite }; var forecast
var forecastOptions = {
"cache": false,
"dataType": "jsonp",
"url": callSite
};
var forecastRequest = $.ajax(forecastOptions);
我这样做是为了我的可读性。我也不想打开“允许url打开”
编辑
这就是我现在拥有的
<?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");
}
?>
它告诉我$content是空的。如果有的话,我还缺少什么。关于所有PHP的cURL函数的文档,您可以参考。另外,我不熟悉JS中的cURL,所以我不确定如何匹配您的选项,但是可以找到PHP的所有cURL选项
例如,它应该如下所示:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$headers = array(
'Content-Type: application/json',
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get the result
$result = curl_exec($ch);
// Get the status
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the session
curl_close($ch);
//Parse the JSON
$result_arr = json_decode($result, true);
多德先生是正确的,但JSONP的使用表明您正在进行跨站点请求。因为PHP通常在服务器端运行,所以您不必担心这一点。您只需要确保您使用的url返回JSON。您可能希望向请求中添加如下标题:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$headers = array(
'Content-Type: application/json',
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get the result
$result = curl_exec($ch);
// Get the status
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the session
curl_close($ch);
//Parse the JSON
$result_arr = json_decode($result, true);
执行请求时,您可以检索响应和HTTP状态,如下所示:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$headers = array(
'Content-Type: application/json',
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get the result
$result = curl_exec($ch);
// Get the status
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the session
curl_close($ch);
//Parse the JSON
$result_arr = json_decode($result, true);
我已经更新了我的代码,但是我没有从请求中得到任何东西。我的代码中遗漏了什么吗?我正在尝试从forecast.io api中提取。您应该尝试从浏览器中使用的url,看看它是否返回任何内容。您还应该从上面的
$httpCode
检查HTTP状态。我打印了\r($httpCode),它给了我0。我获取了调用url并将其放在浏览器url中,以确保它是正确的url,然后将该url粘贴到$request_url的位置。我仍然得到同样的结果。你的反应可能是超时了。您将需要添加超时参数,此处对此进行了详细解释: