I want to post data using PHP curl with basic authentication. Here is my code but I am not able to post using API. But it's working with postman.
$urlp = "URL/sap/opu/odata/sap/ZOSA_UPLOAD_SRV/AccountOrderSet?sap-client=900";
$username = '*****';
$password = '********';
$ch = curl_init ();
curl_setopt($ch,CURLOPT_URL,$urlp);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_MUTE,1);
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
curl_setopt($ch,CURLOPT_USERAGENT,"Proventum Proxy/1.0 (Linux)");
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //times out after 10s
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($chp, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'X-Requested-With: application/json')
);
if ($HTTP_REFERER) {
curl_setopt($ch,CURLOPT_REFERER, $HTTP_REFERER);
}
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$datap_string);
$result = curl_exec ($ch);
print $result;
curl_close($ch);
Here you are,
add to the HTTPHEADER option:
Authorization: Basic base64encode(username:password)
for example:
curl_setopt($chp, CURLOPT_HTTPHEADER, array(
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
'Content-Type: application/json',
'Accept: application/json',
'X-Requested-With: application/json')
);
Related
I'm trying to use Crowd authentication into my website but I keep getting 401 Application failed to authenticate. I don't know why because I am passing in the application username and password correctly. What I have:
$data = array("value" => "APP_PASS");
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://PATH:8095/crowd/rest/usermanagement/2/authentication?username=APP_NAME');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Accept: application/json'
)
);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
You need to use CURLOPT_HTTPAUTH and CURLOPT_USERPWD for your application user:password. Crowd uses ACLs for the requesting application as well as the user authentication you're requesting.
curl -i -u 'APP_USER:APP_PASS' --data '{"value": "USER_PASS"}' https://SERVER_URL/crowd/rest/usermanagement/1/authentication?username=USER_ID --header 'Content-Type: application/json' --header 'Accept: application/json'
My code to add header is:
$apikey="xyz";
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'X-Auth-Token: $apikey'
));
But I am getting an error showing that **This request requires HTTP Authentication.**Now I want to know whether I am right? If Yes why I am getting error.If I am wrong how can do this..
Thankyou,
Use double quotes if you want your variable to be interpolated by PHP, i.e. "X-Auth-Token: $apikey".
For more info see PHP: Strings.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http:www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('name' => 'Bhuvanagiri Gireesh') );
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'key: license-key ';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
There are many ways to achieve it but, this is the easiest way to add headers in curl
This is PayPal's basic sandbox request to get an access token.
$curl = "curl -v https://api.sandbox.paypal.com/v1/oauth2/token
-H 'Accept: application/json' \
-H 'Accept-Language: en_US' \
-u 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp'
-d 'grant_type=client_credentials'";
This is what I've got so far as a conversion but it's failing.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Connection: Keep-Alive',
'Accept-Language: en_US',
'Accept: applictaion/json'
));
curl_setopt($ch, CURLOPT_USERPWD, 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
print 'Running curl';
$res = curl_exec($ch);
echo $res;
curl_close($ch);
Try removing
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Connection: Keep-Alive',
'Accept-Language: en_US',
'Accept: applictaion/json'
));
I copied and pasted your code and just removed the CURLOPT_HTTPHEADER option and it worked!
trying to do the equivalent of this in PHP - and failing :):
curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com;
"123456789" is the API key. The command line statement works fine.
PHP code (does not work):
$urlToGet = "http://www.cnn.com";
$service_url = "http://APIserviceProvider=$urlToGet";
//header
$contentType = 'text/xml'; //probably not needed
$method = 'POST'; //probably not needed
$auth = 'X-abc-AUTH: 123456789'; //API Key
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
//does not work
// curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: ' .
// $contentType . '; auth=' . $auth));
//works! (THANKS #Fratyr for the clue):
curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth));
//this works too (THANKS #sergiocruz):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Some_custom_header: 0',
'Another_custom_header: 143444,12'
));
//exec
$data = curl_exec($ch);
echo $data;
curl_close($ch);
Any ideas?
In order to get custom headers into your curl you should do something like the following:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Some_custom_header: 0',
'Another_custom_header: 143444,12'
));
Therefore the following should work in your case (given X-abc-AUTH is the only header you need to send over):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-abc-AUTH: 123456789' // you can replace this with your $auth variable
));
If you need additional custom headers, all you have to do is add on to the array within the curl_setopt.
I hope this helps :)
Use the following Syntax
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'X-abc-AUTH: 123456789';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$headers[] = 'Host: 202.71.152.126';
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';
$headers[] = 'X-MicrosoftAjax: Delta=true';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth
));
You set only one request header, not the two you wanted. You could do it for example like this:
// input
$urlToGet = "http://www.cnn.com";
// url
$service_url = sprintf("http://APIserviceProvider=%s", urlencode($urlToGet));
//header
$contentType = 'Content-type: text/xml'; //probably not needed
$auth = 'X-abc-AUTH: 123456789'; //API Key
$method = 'POST'; //probably not needed
// curl init
$ch = curl_init($service_url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => [
$contentType,
$auth,
],
]);
// curl exec
$data = curl_exec($ch);
curl_close($ch);
// output
echo $data;
(change the service url to the right one to get this to work)
How can i send a CURL request mentioned below in PHP? What functions i will use in php?
$ curl -H 'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0' \
-H 'Accept: application/json' \
'http://example.sifterapp.com/api/projects'
I have tried this code.. but its not working..
Please do the needful
$curlString = "";
$curlString .= "-H \"X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0\" \";
$curlString .= "-H \"Accept: application/json\" \";
$url="http://example.sifterapp.com/api/projects";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlString);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}
You do not use correctly CURLOPT_HTTPHEADER. From the manual:
http://php.net/manual/en/function.curl-setopt.php
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the
format array('Content-type: text/plain', 'Content-length: 100')
So you would need:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0',
'Accept: application/json',
));