如何使用 X-HTTP-Method-Override 和 PHP curl 请求进行 POST?

发布于 2024-12-10 18:05:37 字数 761 浏览 0 评论 0 原文

我正在使用 Google Translate API,并且有可能发送大量要翻译的文本。在这种情况下,Google 建议执行以下操作:

如果您想发送更多数据,也可以使用POST来调用API 在单个请求中。 POST 正文中的 q 参数必须小于 超过 5K 字符。要使用 POST,您必须使用 X-HTTP-Method-Override 标头告诉 Translate API 处理 作为 GET 请求(使用 X-HTTP-Method-Override: GET)。 Google 翻译 API 文档

我知道如何发出正常的 POST 请求with CURL:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo $response;

但是如何修改标头以使用 X-HTTP-Method-Override?

I'm working with the Google Translate API and there's the possibility that I could be sending in quite a bit of text to be translated. In this scenerio Google recommends to do the following:

You can also use POST to invoke the API if you want to send more data
in a single request. The q parameter in the POST body must be less
than 5K characters. To use POST, you must use the
X-HTTP-Method-Override header to tell the Translate API to treat the
request as a GET (use X-HTTP-Method-Override: GET). Google Translate API Documentation

I know how to make a normal POST request with CURL:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo $response;

But how do I modify the header to use the X-HTTP-Method-Override?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗笺 2024-12-17 18:05:37
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET') );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET') );
娜些时光,永不杰束 2024-12-17 18:05:37

http://php.net/manual/en/function.curl-setopt.php

CURLOPT_HTTPHEADER

要设置的 HTTP 标头字段数组,格式为 array('Content-type: text/plain', 'Content-length: 100')

因此,

curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));

http://php.net/manual/en/function.curl-setopt.php

CURLOPT_HTTPHEADER

An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

Thus,

curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
月棠 2024-12-17 18:05:37

使用 CURLOPT_HTTPHEADER 选项从字符串数组中添加标头

use the CURLOPT_HTTPHEADER option to add a header from a string array

葵雨 2024-12-17 18:05:37

对我来说还不够,我需要使用 http_build_query 来处理我的数组发布数据
我的完整示例:

  $param = array(
    'key'    => 'YOUR_API_KEY_HERE',
    'target' => 'en',
    'source' => 'fr',
    "q" => 'text to translate'
    );
    $formData = http_build_query($param);
    $headers = array( "X-HTTP-Method-Override: GET");
    $ch=curl_init();

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS,$formData);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
    curl_setopt($ch, CURLOPT_REFERER, 'http://yoursite'); //if you have refere domain restriction for your google API KEY
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2');
    $query = curl_exec($ch);
    $info = curl_getInfo($ch);
    $error = curl_error($ch);
    $data  = json_decode($query,true);

    if (!is_array($data) || !array_key_exists('data', $data)) {
     throw new Exception('Unable to find data key');
    }
    if (!array_key_exists('translations', $data['data'])) {
     throw new Exception('Unable to find translations key');
    }
    if (!is_array($data['data']['translations'])) {
     throw new Exception('Expected array for translations');
    }
    foreach ($data['data']['translations'] as $translation) {
     echo $translation['translatedText'];
    }

我在这里找到了此帮助 https://phpfreelancedeveloper.wordpress.com/2012/06/11/translated-text-using-the-google-translate-api-and-php-json-and-curl/
希望有帮助

Not enough for me , i need to use http_build_query fo my array post data
my full example :

  $param = array(
    'key'    => 'YOUR_API_KEY_HERE',
    'target' => 'en',
    'source' => 'fr',
    "q" => 'text to translate'
    );
    $formData = http_build_query($param);
    $headers = array( "X-HTTP-Method-Override: GET");
    $ch=curl_init();

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS,$formData);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
    curl_setopt($ch, CURLOPT_REFERER, 'http://yoursite'); //if you have refere domain restriction for your google API KEY
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2');
    $query = curl_exec($ch);
    $info = curl_getInfo($ch);
    $error = curl_error($ch);
    $data  = json_decode($query,true);

    if (!is_array($data) || !array_key_exists('data', $data)) {
     throw new Exception('Unable to find data key');
    }
    if (!array_key_exists('translations', $data['data'])) {
     throw new Exception('Unable to find translations key');
    }
    if (!is_array($data['data']['translations'])) {
     throw new Exception('Expected array for translations');
    }
    foreach ($data['data']['translations'] as $translation) {
     echo $translation['translatedText'];
    }

I found this help here https://phpfreelancedeveloper.wordpress.com/2012/06/11/translating-text-using-the-google-translate-api-and-php-json-and-curl/
Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文