0

I have been writing a RESTful API for my Laravel application. I can submit data using cURL in Terminal with the following line;

curl -i --user admin@admin.com:qwertyuiop -d "data=somedata" https://www.xxxxxxxxxxxxx.co.uk/app/api/v1/clients

but when i try and do the same in cURL using PHP it gives me a 403 forbidden error.

$url = "https://www.xxxxxxxxxx.co.uk/app/api/v1/clients";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "admin@admin.com:qwertyuiop");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=mydata");
$output = curl_exec($ch);
curl_close($ch);
print_r($output);

Any ideas what i'm missing from my PHP cURL script? Thanks

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
adam Kearsley
  • 951
  • 2
  • 13
  • 23

1 Answers1

0

It's been almost 6 years since this question asked. But I'm sure this answer will solve your and probably another problem with this. This code also solved my problem.

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://example.com",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_TIMEOUT => 30000,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        // Set Here Your Requested Headers
        'Content-Type: application/json',
    ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    dd($response);    //this line is my custom code
}

As explained in this amazing website, where I found this code about Laravel POST using cURL

Dani Fadli
  • 353
  • 4
  • 18