-1

I want to cURL the following request in PHP:

curl --location --request GET 'xxx.xxxxxad.com/api/rest/issues?project_id=4' -H 'Authorization:xxxxxxxxxxxxxxxxxxxxxxxxxxxHTb'

The code that I implemented was:

<?php  

$url = "xxx.xxxxxad.com/api/rest/issues?project_id=4";  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, array('Authorization:xxxxxxxxxxxxxxxxxxxxxxxxxxxHTb'));  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  

$output = curl_exec($ch);  

echo $output;

curl_close($ch);  

?>

I cannot find a way to include --location in the request without which the data cannot be fetched.

  • Your question is not clear enough. please, check first sentence you used. – Hamed Baziyad Jan 22 '20 at 05:15
  • 1
    Go read up on what `--location` _does_, then go read the manual page for PHP curl_setopt, and try to figure out what the equivalent is …? You probably want `CURLOPT_FOLLOWLOCATION` – 04FS Jan 22 '20 at 07:46
  • Also go change your access key, because it shouldn't be hard for someone who _really_ wanted to, or is just _really_ bored, to figure out which domain you're using, and now they have your access key. (editing your post won't suffice - SO has an edit history) – Mike 'Pomax' Kamermans Jan 22 '20 at 08:51

1 Answers1

0

--request GET roughly translates to CURLOPT_HTTPGET=>1 (actually the strict translation is CURLOPT_CUSTOMREQUEST, but don't do that, it's usually a bad idea leading to bugs down the road if/when you try to re-use the curl handle for other stuff in the future and forget to reset it - CUSTOMREQUEST is bug-prone, as a rule of thumb: avoid it where possible.)

also CURLOPT_HEADER does something completely different (you can read about it here: https://curl.haxx.se/libcurl/c/CURLOPT_HEADER.html ) , what you need to actually set custom headers is CURLOPT_HTTPHEADER, in short:

curl_setopt_array($ch, array(
    CURLOPT_HTTPGET => 1,
    CURLOPT_HTTPHEADER => array(
        'Authorization:xxxxxxxxxxxxxxxxxxxxxxxxxxxHTb'
    ),
    CURLOPT_URL => 'xxx.xxxxxad.com/api/rest/issues?project_id=4'
));
hanshenrik
  • 19,904
  • 4
  • 43
  • 89