简体   繁体   中英

How do I make a POST using X-HTTP-Method-Override with a PHP curl request?

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?

curl_setopt($ch, 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'));

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM