1

出于安全原因,我的虚拟主机不支持 file_get_contents,但支持使用 cURL。谁能告诉我现在我会使用 curl 转换这个短代码吗?我已经尝试了好几天没有运气,所以任何帮助将不胜感激!

<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b2b4a1ad0a889006/geolookup 
/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; 
echo "Current temperature in ${location} is: ${temp_f}\n";
?>
4

1 回答 1

0

我建议你使用套接字

<?php
$fp = stream_socket_client("tcp://api.wunderground.com:80", $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "GET /api/b2b4a1ad0a889006/geolookup/conditions/q/IA/Cedar_Rapids.json HTTP/1.0\r\nHost: api.wunderground.com\r\nAccept: */*\r\n\r\n");
    while (!feof($fp)) {
        echo json_decode(fgets($fp));
    }
    fclose($fp);
}
?>
于 2013-05-19T19:54:05.567 回答