1

Here is my problem. I'm running a service on a remote machine working perfectly. The way to get the results from the machine is via api.

curl -X GET http://ip:777/api \
     -d "r=request" 

It works perfectly on the terminal. Moreover, it works perfectly, if the request query is short. But, it turns into a huge problem once, it passes some length(1800-2000 characters and I need 7k-8k chars).

However, I can't "transliterate" the curl code into PHP. If there is anyone with any idea how to do it please show me the way. As much as, I'm aware, this is a curl GET method with REQUEST BODY.

$long_query = "r=" . $request;
// set the api
curl_setopt($ch, CURLOPT_URL, 'http://ip:777/api');
// i want to get the return
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 2min+ timeout as to make sure that I get a result
curl_setopt($ch, CURLOPT_TIMEOUT, 140);

// Set request method to GET by 0'ing the POST method
curl_setopt($ch, CURLOPT_POST, 0);

// Set query data here with CURLOPT_POSTFIELDS
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($long_query));

$content = curl_exec($ch);
curl_close($ch);
echo $content;

What am I doing wrong in here? If someone knows, please explain as if you are teaching a year old. Thanks in advance!

Ali
  • 19
  • 4
  • Does this answer your question? [PHP cURL GET request and request's body](https://stackoverflow.com/questions/17230246/php-curl-get-request-and-requests-body) – Zeitounator Jan 12 '20 at 08:46
  • @Zeitounator wasn't of any help. already checked it out. – Ali Jan 12 '20 at 08:51
  • check this also: https://stackoverflow.com/a/2659995/3599237 – Akam Jan 12 '20 at 09:22
  • @Akam there is no problem from server's end when I'm using terminal from my local machine to send the request. curl works perfectly through the terminal. It's just I can't make it work with the php – Ali Jan 12 '20 at 09:32
  • Is there any way to make a POST request? Because there is a limitation for GET url size. Though it depends on servers or browsers such as apache has default value of 8190 bytes for LimitRequestLine. https://httpd.apache.org/docs/2.4/mod/core.html#limitrequestline – unclexo Jan 12 '20 at 09:33
  • @unclexo POST is disabled by default for api. Unfortunately, its not running on apache but jetty. and i have no idea how to change those defaults at jetty – Ali Jan 12 '20 at 09:47

2 Answers2

0

I think the following doc would help you understand how GET method works. This is from RFC 7231

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

For more details, please refer to this answer.

Community
  • 1
  • 1
unclexo
  • 3,691
  • 2
  • 18
  • 26
0

Alright, here we go with the proper answer.

on terminal,

curl -X GET http://ip:777/api \
     -d "r=request" 

works perfectly. However, the problem with converting that to php curl is quite troublesome while very easy at the same time.

I've read through every stack problem regarding this and no-one has provided a clear answer to the problem. I'm not sure the reason behind it but as a generous person I'll give out the code so that anyone in the future facing this rare problem will solve it out easily.

Long story short,

curl -X GET -d is the same as curl -X POST -H "X-HTTP-Method-Override: GET".

The actual request is POST but THE SERVER will consider it as a GET. This way you won't face the LONG URI problem.

$long_query = "r=" . $request;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"ip:777/api");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $long_query);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'X-HTTP-Method-Override: GET',
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
curl_close ($ch);

var_dump($server_output);

I've set the timeout to 140 as the query is long and it takes a bit of time for the server to go through it and respond (in my case its a json). Nevertheless, I've added var_dump so that anyone who uses it in the future might see if its a serialized array or whatever.

Good luck!

Ali
  • 19
  • 4