Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 430
ashansky Avatar asked Oct 20 '11 14:10

ashansky


People also ask

How do you use override in X HTTP?

The X-HTTP-Method-Override HTTP header works somewhat similar to a hack. You can add the header with a value of either PUT or DELETE when invoking your Web API via JavaScript or via an XMLHttpRequest object from a web browser using an HTTP POST call.

Which function in PHP do you use to set curl options?

Parameters ¶ A cURL handle returned by curl_init(). The CURLOPT_XXX option to set. The value to be set on option .

What is http override?

For tighter security, some firewalls do not allow HTTP PUT or DELETE traffic. To accommodate this restriction, you can send these requests in two ways: Use the X-Method-Override or X-HTTP-Method-Override HTTP header fields to channel a PUT or DELETE request through a POST request.

Does PHP Curl wait for response?

AFAIK, it will wait for a response before continuing running the script. Try adding if ($ret===false) echo curl_error($curl); after the $ret = curl_exec...


2 Answers

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET') );
like image 111
Mob Avatar answered Oct 23 '22 15:10

Mob


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'));
like image 1
Amber Avatar answered Oct 23 '22 14:10

Amber