相关疑难解决方法(0)

在获取Google API数据时,cURL无法正常工作

所以,我正在尝试使用cURL获取API数据,但是我从下面的代码中的else语句中得到消息"fail".API调用是用于获取坐标的Google地理编码.

代码:

 <?php 
    require_once('../db.php');
    $api_key = "somekey";
    $sqlQuery = mysql_query("SELECT `County` FROM `table`"); 
    $ch = curl_init();


    /* Fetch county */ 
    while($rows = mysql_fetch_array($sqlQuery))  { 
        $countyArr = $rows['County']; 

        /* Call google API and save coordinates for each county */ 
        curl_setopt ($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address=".$countyArr.",+CA&key=".$api_key."");
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        $json= curl_exec($ch, true);
        $obj = json_decode($json);

        if ($obj->status == "OK") {
            $lat = $obj->results->location->lat;
            $lng = $obj->results->location->lng;
            echo $lat;
        } else {
            echo "fail";
        }
    }
    curl_close($ch);
?> 
Run Code Online (Sandbox Code Playgroud)
  • 我打算get_file_contents() …

php api json curl google-maps

3
推荐指数
1
解决办法
2652
查看次数

标签 统计

api ×1

curl ×1

google-maps ×1

json ×1

php ×1