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

4

5

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?

Lenlena answered 20/10, 2011 at 14:50 Comment(0)
T
6
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET') );
Tract answered 20/10, 2011 at 14:58 Comment(1)
Did this work for you? I'm still getting BAD REQUEST. ;(Suspend
R
1

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'));
Raye answered 20/10, 2011 at 14:56 Comment(0)
N
0

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

Neville answered 20/10, 2011 at 14:55 Comment(0)
H
0

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

Havener answered 24/3, 2015 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.