我在对 Google 翻译 API 执行 cUrl 时找不到文件

I am getting file not found when doing a cUrl to Google translate API

请告诉我这里缺少什么?我一直收到错误 404,找不到文件

    $apiKey = "MY-KEY-IS-HERE";
    $apiUri = "https://www.googleapis.com/language/translate/v2";

    $text = "Hello world";
    $source = "EN";
    $target = "NL";

    if (!$target || !$text) {
        echo "Noting to translate";
    } else {
        $fields = array(
            "key" => $apiKey,
            "source" => $source,
            "target" => $target,
            "q" => $text
        );
        $fieldsString = "";
        foreach($fields as $key=>$value) {
            $fieldsString .= $key . '=' . $value . '&';
        }
        rtrim($fieldsString, '&');
        echo $fieldsString;

        $handle = curl_init($apiUri);
        curl_setopt($handle, CURLOPT_URL, $apiUri);
        curl_setopt($handle, CURLOPT_POST, true);
        curl_setopt($handle, CURLOPT_POSTFIELDS, $fieldsString);
        curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fieldsString)));
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($handle);

        $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
        if($responseCode != 200) {
            echo 'Fetching translation failed! Server response code: ' . $responseCode . '<br>';
            echo $response;
        }
        else {
            echo $response;
        }
        curl_close($handle);

当我将 $string 数组放入 CURL_POSTFIELDS 时,没有任何变化。还尝试添加一个?在 URI 中标记或添加尾部斜杠。继续得到相同的错误。为了测试,我还删除了以下行:

        curl_setopt($handle, CURLOPT_POSTFIELDS, $fieldsString);
        curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fieldsString)));

然后收到一条错误消息,指出 headers 中缺少长度。谁会说它正确地到达了 api..???

让它像这样工作:

    $apiKey = "MY-KEY-IS-HERE";
    $apiUri = "https://www.googleapis.com/language/translate/v2";

    $text = "Hello world";
    $source = "EN";
    $target = "NL";

    if (!$target || !$text) {
        echo "Noting to translate";
        exit;
    } else {
        $fields = array(
            "key" => $apiKey,
            "source" => $source,
            "target" => $target,
            "q" => $text
        );
        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $apiUri);
        curl_setopt($handle, CURLOPT_POST, true);
        curl_setopt($handle, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($handle, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: GET"));
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($handle);

        $responseDecoded = json_decode($response, true);

        $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
        if($responseCode != 200) {
            echo 'Fetching translation failed! Server response code: ' . $responseCode . '<br>';
            echo $response;
        }
        else {
            echo $responseDecoded['data']['translations'][0]['translatedText'];
        }
        curl_close($handle);

你浪费了很多时间来做 CURL 可以自动为你做的事情,而且更可靠:

    $handle = curl_init($apiUri);
    curl_setopt($handle, CURLOPT_URL, $apiUri);  // redundant

您已经通过 curl_init() 设置了 URI,没有必要再次设置它。

然后你也建立你自己的 post body,设置 content-length,等等等等。跳过所有这些。你只需要

    $fields = array(
        "key" => $apiKey,
        "source" => $source,
        "target" => $target,
        "q" => $text // note LACK of urlencode here
    );
    curl_setopt($handle, CURLOPT_POSTFIELDS, $fields);

CURL 会很乐意接受您的数组并根据需要对其进行转换,并设置任何合适的 headers。