【问题标题】:Google Cloud Vision API on PHP AppEnginePHP AppEngine 上的 Google Cloud Vision API
【发布时间】:2016-08-24 23:27:41
【问题描述】:

一段时间以来,我一直在使用 Google Cloud Vision api 和托管在私有 VPS 上的 php 应用程序,没有出现任何问题。我正在将应用程序迁移到 Google AppEngine,但现在遇到了问题。

我正在使用 CURL 发布到 API,但它在 AppEngine 上失败了。我启用了计费,并且其他 curl 请求正常工作。有人提到对 googleapis.com 的调用在 AppEngine 上不起作用,我需要以不同的方式访问 API。我无法在网上找到任何资源来确认这一点。

以下是我的代码,返回 CURL 错误 #7,连接主机失败。

$request_json = '{
            "requests": [
                {
                  "image": {
                    "source": {
                        "gcsImageUri":"gs://bucketname/image.jpg"
                    }
                  },
                  "features": [
                      {
                        "type": "LABEL_DETECTION",
                        "maxResults": 200
                      }
                  ]
                }
            ]
        }';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://vision.googleapis.com/v1/images:annotate?key='.GOOGLE_CLOUD_VISION_KEY);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request_json);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status != 200) {
    die("Error: $status, response $json_response, curl_error " . curl_error($curl) . ', curl_errno ' . curl_errno($curl));
}
curl_close($curl);
echo '<pre>';
echo $json_response;
echo '</pre>';

【问题讨论】:

    标签: php google-app-engine google-cloud-vision


    【解决方案1】:

    我将代码切换为使用 URLFetch (file_get_contents) 而不是 CURL。到目前为止工作得很好。我仍然不确定为什么 CURL 不起作用。

    【讨论】:

    • 因为您不能使用 cURL 连接到 google 拥有的网站。 (这是远程套接字 API 的限制)
    【解决方案2】:

    在 PHP 中对 Google API 的 curl 请求失败,因为 curl 使用 Sockets API 并且 Google IP 被套接字阻止。这个限制记录在Limitations and restrictions:

    私有、广播、多播和 Google IP 范围被阻止


    要发送您描述的POST 请求,您可以使用PHP 的流处理程序,提供必要的上下文来发送数据。我已经修改了Issuing HTTP(S) Requests 中显示的示例以满足您的要求:

    <!-- language: lang-php -->
    
    $url = 'https://vision.googleapis.com/v1/images:annotate';
    $url .= '?key=' . GOOGLE_CLOUD_VISION_KEY;
    
    $data = [
        [
            'image' => [
                'source' => [
                    'gcsImageUri' => 'gs://bucketname/image.jpg'
                ]
             ],
             'features' => [
                 [
                     'type' => 'LABEL_DETECTION',
                     'maxResults' => 200
                 ]
             ]
        ]
    ];
    
    $headers = "accept: */*\r\nContent-Type: application/json\r\n";
    
    $context = [
        'http' => [
            'method' => 'POST',
            'header' => $headers,
            'content' => json_encode($data),
        ]
    ];
    $context = stream_context_create($context);
    $result = file_get_contents($url, false, $context);
    

    如果您决定使用 OAuth 等 API 密钥以外的身份验证方式,我还建议您阅读 Asserting identity to Google APIs

    【讨论】:

    • 此代码不起作用。知道为什么吗?警告:file_get_contents(vision.googleapis.com/v1/images:annotate?key=*****************):打开流失败:HTTP 请求失败! HTTP/1.0
    • 是否有更详细的日志记录可用?你提供了什么error_reporting() 级别?您可以测试以这种方式向任何其他端点发送POST 请求吗?尽管消息很简洁,但它确实表明根本没有建立任何联系,更不用说拒绝了。如果是这种情况,问题可能是请求函数而不是 Vision API 服务器。
    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 2023-03-02
    • 2019-03-13
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多