0

I have an Axios HTTP GET request I'd to convert to PHP cURL.

Axios Request

axios({
    method: 'get',
    url: 'https://api.sample.com/123456789/',
    data: {
        apikey: '987654321',
        id: '123123',
    }
}).then(function ( response ) {
    console.log( response );
});

How do I make this request in PHP cURL, sending the apikey and id data, then echoing the response?

cURL I Was Trying

<?php
$url = 'https://api.sample.com/123456789/';
$body_arr = [
    'apikey' => '987654321',
    'id' => '123123',
];

$data = http_build_query($body_arr);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

$result_arr = json_decode($result, true);

echo '<pre>';
var_dump( $result_arr );
echo '</pre>';
?>

Result

NULL
GTS Joe
  • 3,612
  • 12
  • 52
  • 94
  • Does the API definitely return JSON? What is the content of `$result`. If you're looking to replicate the axios code precisely, that version doesn't try to parse the response as JSON, so its unclear why you've added that step in the php version – ADyson Mar 02 '22 at 00:12
  • 1
    Also you're sending a GET, so adding POST fields makes no sense. And it's not equivalent- I expect axios adds those data fields to the URL when it's a GET, rather than putting them in the request body (many http agents don't recognise a request body in a GET...it would be highly unconventional, at best). You should do the same in the php/cURL version. – ADyson Mar 02 '22 at 00:14
  • Good point about sending POST data in a GET request. How can I send my array using cURL without putting it in the URL query string? – GTS Joe Mar 02 '22 at 00:18
  • @ADyson Looks like GET requests are valid with a POST body: https://stackoverflow.com/questions/17230246/php-curl-get-request-and-requests-body#31579115 – GTS Joe Mar 02 '22 at 00:21
  • They certainly are valid, as I mentioned it's just unconventional and some clients don't let you use them (which is technically wrong but semantically logical) – ADyson Mar 02 '22 at 00:43
  • `How can I send my array using cURL without putting it in the URL query string`...what do you mean? You're already doing that. I suspect you may need to change it. But first check what's in $result when you run the current version of the php, then we can check the potential JSON issue – ADyson Mar 02 '22 at 00:45
  • @ADyson it's so rare in fact that Axios appears to bug out in this situation (see my answer below :P ) – hanshenrik Mar 02 '22 at 13:10

1 Answers1

2

when you set data together with method: 'GET', axios will set Content-Type: application/json and.. completely ignore the post data. so the correct translation is:

<?php
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => 'https://api.sample.com/123456789/',
    CURLOPT_HTTPGET => 1,
    CURLOPT_HTTPHEADER => array(
        // this is not correct, there is no content-type,
        // but to mimic Axios's behavior with setting `data` on GET requests, we send this
        // incorrect header:
        'Content-Type: application/json'
    )
));
curl_exec($ch);
  • fwiw this feels like an axios bug, it wouldn't surprise me if this changes in a future version of axios.
hanshenrik
  • 19,904
  • 4
  • 43
  • 89