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
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
Related
I'm trying to convert the line below to be used with PHP whilst also learning how to use CURL!
$ curl -X POST -d 'key=YOUR_KEY&secret=YOUR_SECRET' "https://api.example.co.uk/authenticate" -H "Content-Type: application/x-www-form-urlencoded"
Bellow is what I have so far, however I keep getting HTTP ERROR 403 UnauthorizedException accessing service error, so I think the key and secret and not being sent correctly.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.example.co.uk/authenticate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"key\": \"MYKEY\",\"secret\": \"MYSECRET\"}",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Use this tool as it save so much time - https://incarnate.github.io/curl-to-php/
Generated this:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.co.uk/authenticate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "key=YOUR_KEY&secret=YOUR_SECRET");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Easy way is to use an array for header variable and assign api_key or secret key through array and pass that variable into curls->CURLOPT_HTTPHEADER
// Collection object
$ch = curl_init($url);
$headers = array(
"APIKEY: PUT_HERE_API_KEY",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml"
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
$result = curl_exec($ch); // execute
$result;
//show response
curl_close($ch);
I am trying to create a HTTP PUT request with cURL and I can't make it work. I've read many tutorials but none of them actually worked. Here's my current code:
$filedata = array('metadata' => $rdfxml);
$ch = curl_init($url);
$header = "Content-Type: multipart/form-data; boundary='123456f'";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata));
$returned = curl_exec($ch);
if (curl_error($ch))
{
print curl_error($ch);
}
else
{
print 'ret: ' .$returned;
}
I've also tried using PHP PEAR but got the same result. The problem is that the repository says that no metadata has been set. I really need help! Thanks!
Just been doing that myself today... here is code I have working for me...
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
Using Postman for Chrome, selecting CODE you get this... And works
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://blablabla.com/comorl",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n \"customer\" : \"con\",\n \"customerID\" : \"5108\",\n \"customerEmail\" : \"jordi#correo.es\",\n \"Phone\" : \"34600000000\",\n \"Active\" : false,\n \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"x-api-key: whateveriyouneedinyourheader"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
In a POST method, you can put an array. However, in a PUT method, you should use http_build_query to build the params like this:
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
You have mixed 2 standard.
The error is in $header = "Content-Type: multipart/form-data; boundary='123456f'";
The function http_build_query($filedata) is only for "Content-Type: application/x-www-form-urlencoded", or none.
I'm attempting to send a cURL command to send a push notification. I'm pretty new to cURL and cannot seem to get the command to run.
Here is the cURL command line:
curl --header "Authorization: key=AAAAVD27CWY:APA91bE7YdKYiqTmQhErf0E3gm8lbgNt2KP5-xPQf83V7m8eKsa0ljktOLiGyzzrP0uxVNBHC6cyuJAPejkTyNl1DnoxcajesLvGXIzq3YR1l-wiFvoivRmIUkDvThTsKCJkZMomhEPp" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"fr1l051Pczw:APA91bGkyuA6iKMP6oICJ8NweijQpWTWGuo-inqvpF5-Mety0D7oL_ppvevKKdWPxIo7ev_v5sAWbprk7pEg8kz3cNCivipL9RCR3XlA1caBtahsRtnANZpaU-KYnsdjcGY3Q51xN1ny\"]}"
and here is the PHP cURL I'm trying to execute:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"registration_ids\":[\"fr1l051Pczw:APA91bGkyuA6iKMP6oICJ8NweijQpWTWGuo-inqvpF5-Mety0D7oL_ppvevKKdWPxIo7ev_v5sAWbprk7pEg8kz3cNCivipL9RCR3XlA1caBtahsRtnANZpaU-KYnsdjcGY3Q51xN1ny\"]}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: key=AAAAVD27CWY:APA91bE7YdKYiqTmQhErf0E3gm8lbgNt2KP5-xPQf83V7m8eKsa0ljktOLiGyzzrP0uxVNBHC6cyuJAPejkTyNl1DnoxcajesLvGXIzq3YR1l-wiFvoivRmIUkDvThTsKCJkZMomhEPp";
$headers[] = "Content-Type: \"application/json\"";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I'm running this with Google's Firebase Cloud Messaging.
I can execute the command on my server, so I know at least that works.
Running UBUNTU 16.04 and Apache.
I am running SSL on the server and have tried adding:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Still no luck.
Please use the below PHP Curl Request
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://android.googleapis.com/gcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"registration_ids\":[\"fr1l051Pczw:APA91bGkyuA6iKMP6oICJ8NweijQpWTWGuo-inqvpF5-Mety0D7oL_ppvevKKdWPxIo7ev_v5sAWbprk7pEg8kz3cNCivipL9RCR3XlA1caBtahsRtnANZpaU-KYnsdjcGY3Q51xN1ny\"]}",
CURLOPT_HTTPHEADER => array(
"authorization: key=AAAAVD27CWY:APA91bE7YdKYiqTmQhErf0E3gm8lbgNt2KP5-xPQf83V7m8eKsa0ljktOLiGyzzrP0uxVNBHC6cyuJAPejkTyNl1DnoxcajesLvGXIzq3YR1l-wiFvoivRmIUkDvThTsKCJkZMomhEPp",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
hello i'm using code used by other people who supposedly have gotten it to work and have gotten their token information retrieved. The code is as follows:
$ch = curl_init();
$clientId = "myclientid";
$secret = "mysecret";
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json->access_token);
}
curl_close($ch); //THIS CODE IS NOW WORKING!
I retrieved this code from Paypal API with PHP and cURL and have seen it implemented in some others peoples code so i assume that it works. However i'm only receiving no response even though i am supplying the correct client id and secret (maybe a recent update has broken this code?).
The guide provided by Paypal on getting the access token is found here-> https://developer.paypal.com/docs/integration/direct/make-your-first-call/
however it demonstrates the solution in cURL and not through the PHP cURL extension so its alittle cryptic to me. Any help?
Well it seems that it is now a requirement to declare what type of SSL Version to use, thus the code above will work when curl_setopt($ch, CURLOPT_SSLVERSION , 6); //tlsv1.2 is inserted.
Just tested the API and I can confirm this code is working :
(No SSL options)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Language: en_US"
),
));
$result= curl_exec($curl);
$array=json_decode($result, true);
$token=$array['access_token'];
echo "<pre>";
print_r($array);
I am trying to create a HTTP PUT request with cURL and I can't make it work. I've read many tutorials but none of them actually worked. Here's my current code:
$filedata = array('metadata' => $rdfxml);
$ch = curl_init($url);
$header = "Content-Type: multipart/form-data; boundary='123456f'";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata));
$returned = curl_exec($ch);
if (curl_error($ch))
{
print curl_error($ch);
}
else
{
print 'ret: ' .$returned;
}
I've also tried using PHP PEAR but got the same result. The problem is that the repository says that no metadata has been set. I really need help! Thanks!
Just been doing that myself today... here is code I have working for me...
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
Using Postman for Chrome, selecting CODE you get this... And works
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://blablabla.com/comorl",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n \"customer\" : \"con\",\n \"customerID\" : \"5108\",\n \"customerEmail\" : \"jordi#correo.es\",\n \"Phone\" : \"34600000000\",\n \"Active\" : false,\n \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"x-api-key: whateveriyouneedinyourheader"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
In a POST method, you can put an array. However, in a PUT method, you should use http_build_query to build the params like this:
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
You have mixed 2 standard.
The error is in $header = "Content-Type: multipart/form-data; boundary='123456f'";
The function http_build_query($filedata) is only for "Content-Type: application/x-www-form-urlencoded", or none.