2013-05-19 55 views
1

出于安全原因,我的网络主机不支持file_get_contents,但确实支持使用cURL。任何人都可以告诉我,现在我会用curl来转换这个短代码吗?我一直在努力尝试好几天,所以任何帮助将不胜感激!使用cURL代替file_get_contents

<?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"; 
?> 
+0

http://www.php.net/manual/en/curl.examples-basic.php几乎总结使用率高达。 – m90

+0

为什么你不使用Weather Underground API? –

+0

$ ch = curl_init(“http://api.wunderground.com/api/b2b4a1ad0a889006/geolookup/q/IA/Cedar_Rapids.json”); $ responce = json_decode(curl_exec($ ch),true); –

回答

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); 
} 
?>