抓取Google API数据时cURL不起作用 [英] cURL not working while fetching Google API data

查看:168
本文介绍了抓取Google API数据时cURL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图使用 cURL ,但是我从下面的代码中的else语句中得到消息失败。
API调用的 Google地理编码用于获取坐标。



代码

 < ;?php 
require_once('../ db.php');
$ api_key =somekey;
$ sqlQuery = mysql_query(SELECT`County` FROM`table`);
$ ch = curl_init(); ($ rows = mysql_fetch_array($ sqlQuery)){
$ countyArr = $ rows ['County'];

$ b / *获取郡* /


/ *调用google API并保存每个县的坐标* /
curl_setopt($ ch,CURLOPT_URL,https://maps.googleapis.com/maps/api/geocode/json ?地址=,+ CA& $ countyArr。 ;关键= $ 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 {
echofail;
}
}
curl_close($ ch);
?>




  • 我打算使用 get_file_contents()早些时候,但它似乎像我的托管已停用该功能。

  • 在php.ini中添加 allow_url_fopen = on 并没有成功。

  • 看起来像我的托管允许cURL ,所以不应该是问题。

  • 我尝试手动进入API调用,并且显示正确的JSON数据。

  • SQL


编辑:


  • 我尝试回显 $ obj-> status $ obj-> results-> ;位置 - > lat
    else语句中,没有任何显示。所以 $ obj 好像是NULL


解决方案

验证证书似乎失败,可以禁用CA验证



要关闭证书验证(对于对等验证和主机验证),请设置以下选项: / p>

  curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,false);

示例

如何禁用证书验证

  $ address =Ontario,CA; 
$ apiKey =;

$ url =https://maps.googleapis.com/maps/api/geocode/json?address=。 $地址; //。 & key =。 $ apiKey;

$ ch = curl_init();
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); //禁用SSL验证
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_URL,$ url);
$ result = curl_exec($ ch);
curl_close($ ch);

$ json = json_decode($ result);
打印json_encode($ result,JSON_PRETTY_PRINT);


So, I'm trying to fetch API-data using cURL, but I get the message "fail" from the else-statement in the code below. The API call is Google geocode for fetching coordinates.

The code:

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

  • I intended to use get_file_contents() earlier but it seems like my hosting has deactivated that function.
  • Adding allow_url_fopen = on to php.ini didn't do the trick.
  • It seems like my hosting allows cURL, so that shouldn't be the problem.
  • I've tried to manually go to the API-call and I get a webpage showing the correct JSON-data.
  • The SQL-query seems to be working fine too.

Edit:

  • I tried echoing $obj->status and $obj->results->location->lat in the else-statement, nothing showed up. So $obj seems to be NULL

解决方案

It seems it fails while verifying certificate, you could disable CA verification

To turn off certificate verification (for both peer and host verification) set the following options:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

Example

How to disable certificate verification

$address = "Ontario,CA";
$apiKey = "";

$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address; //. "&key=" . $apiKey;

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);

$json = json_decode($result);
print json_encode($result, JSON_PRETTY_PRINT);

这篇关于抓取Google API数据时cURL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆