1

翻訳に Google API を使用するアプリケーションを構築しています。

私は問題なく接続できます。ただし、cURL で 403 エラーの内容を読み取れるようにする必要があります。

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured"
   }
  ],
  "code": 403,
  "message": "Access Not Configured"
 }
}

Google API が 403 エラー ヘッダーと一緒に吐き出すデータを無視しないように cURL を設定するにはどうすればよいですか。エラーヘッダーが原因で、cURL が API が表示している JSON を完全に無視しているようです。私はすべてを検索しましたが、接続方法に関する質問へのリンクしか見つかりませんでした. 何か案は?

スクリプトが何をすべきかを判断できるように、エラーを PHP に取り込むことが重要です。ブラウザで API 呼び出しの URL を開くと、エラーが表示されるのが面倒です。


アップデート:

var_dump(curl_getinfo($ch)); 与えます:

array(20) { ["url"]=> string(48) "https://www.googleapis.com/language/translate/v2" ["content_type"]=> string(24) "text/html; charset=UTF-8" ["http_code"]=> int(404) ["header_size"]=> int(360) ["request_size"]=> int(355) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.044256) ["namelookup_time"]=> float(0.001419) ["connect_time"]=> float(0.009028) ["pretransfer_time"]=> float(0.034619) ["size_upload"]=> float(102) ["size_download"]=> float(9) ["speed_download"]=> float(203) ["speed_upload"]=> float(2304) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(102) ["starttransfer_time"]=> float(0.044179) ["redirect_time"]=> float(0) } 

しかし、これには私が取得しようとしている JSON が含まれていません!


PHP cURL ルーチンの例:

$params = http_build_query($params_ar); // params contains all the connection auth and config.... its all good.

$api_url = "https://www.googleapis.com/language/translate/v2";

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

// find the 403 error JSON!!!!!

var_dump(curl_getinfo($ch));

echo "<hr>the url: ";

echo $api_url."?".$params;

echo "<hr>the body: ";

echo $body;

echo "<hr>";

var_dump(curl_error($ch));

echo "<hr>";

curl_close($ch);

// WHERE IS IT???????

exit;
4

0 に答える 0