I am trying send a curl request to a URL which requires HTTP_ORIGIN to be set, I have this so far...
$headers = array(
'Origin: www.myorigin.com',
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER, true,
CURLOPT_HTTPHEADER, $headers,
CURLOPT_URL => 'http://www.example.com',
CURLOPT_USERAGENT => 'Sample Request'
));
$resp = curl_exec($curl);
curl_close($curl);
This is giving me an error on the server side of Undefined index: HTTP_ORIGIN so it doesn't look like it is passing the origin through.
Have I set this up correctly?
You use coma , instead of => into your array, so CURLOPT_HTTPHEADER is a value of the array, instead of a key.
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => true, // << HERE
CURLOPT_HTTPHEADER => $headers, // << HERE
CURLOPT_URL => 'http://www.example.com',
CURLOPT_USERAGENT => 'Sample Request'
));
Related
I'm running a cURL post request that is appearing to hit the url and return a response. However, I'm led to believe that I'm not handling the request output properly and it's throwing an error that I don't understand. I'm extremely new to cURL and if anyone could point me in the right direction, I'd appreciate it.
$curl = curl_init();
$headers = array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'XXX',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
client_id => 'XXX',
client_secret => 'XXX',
member_id => 'XXX'
],
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
]);
$resp = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($resp, true));
This results in the errors that follows:
{"errors":{"":["Input string '--------------------------cec6101ba64bcb7f' is not a valid number. Path '', line 1, position 42."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80031695-0002-ef00-b63f-84710c7967bb"}
$curl = curl_init();
$headers = array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
);
//////
Adding this and using json_encode allowed for the request to be properly sent with the required id's and secret tokens.
//////
$data = array(
"ClientId" => "$api_clientId" ,
"ClientSecret" => "$api_clientSecret",
"MemberId" => "$api_memberId"
);
$data_string = json_encode($data);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cors-anywhere.herokuapp.com/https://staging.micromerchantsystems.com/authenticationservice/api/GateKeeper/createtoken',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
]);
$resp = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($resp, true));
Using curl to call API GET method with headers
but header not working
Response -- Failed to connect to 52.172.133.9: Permission denied
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "25002",
CURLOPT_URL => "https://api.eko.in:25002/ekoicici/v1/customers/mobile_number:8734818474?initiator_id=8734818474",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => "CURL_HTTP_VERSION_1_1",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"developer_key: $developerKey",
"secret-key: $secret_key",
"secret-key-timestamp: $secret_key_timestamp"
)
));
$response = curl_exec($curl);
var_dump($response);
$err = curl_error($curl);
var_dump($err);
var_dump("developerKey--".$developerKey);
var_dump("secret_key--".$secret_key);
var_dump("secret_key_timestamp--".$secret_key_timestamp);
curl_close($curl);
when hitting the request on this URL it requires key that's pass on the header but the header does not set on curl.
how to fix that's a problem...
When trying to revoke the oauth access_token for Box, I get the error : Client id was not found in the headers or body
This is the curl-command (which works fine) :
curl https://api.box.com/oauth2/revoke -d 'client_id=CLIENT_ID&client_secret=CLIENT_SECRET&token=access_token' -X POST
When trying the same with php curl, I get the error.
<?php
$revokeurl="https://api.box.com/oauth2/revoke";
$dataq = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'token' => $access_token
);
$dataqjson= json_encode($dataq);
$headers=array(
"Content-Type: application/json"
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $revokeurl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $dataqjson,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
));
$response = curl_exec($curl);
$json_response = json_decode( $response, TRUE );
curl_close($curl);
?>
Why is my POST with json body not correct ?
Data should be set as application/x-www-form-urlencoded rather than JSON-encoded. Example:
<?php
$revokeurl = "https://api.box.com/oauth2/revoke";
$dataq = array(
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'token' => 'access_token',
);
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => $revokeurl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($dataq),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
)
);
$response = curl_exec($curl);
$json_response = json_decode($response, false);
print_r($json_response);
curl_close($curl);
I'm trying to access a third-party API with PHP.
Their command reference is like so:
curl "https://api.example.com/api/" \
-H "Authorization: BEARER-TOKEN" \
-F 'data={"language":"EN","latitude":12.345,"longitude":12.345}'
I've tried:
$url = 'https://api.example.com/api/';
$header = array(
'Content-type: application/json',
'Authorization: ' . $bearertoken
);
$fields = 'data={"language":"EN","latitude":"'.$latitude.'","longitude":"'.$longitude.'"}';
// I've tried this too:
$fields = array('data' => '{"language":"EN","latitude":"'.$latitude.'","longitude":"'.$longitude.'"}');
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_HTTPHEADER => $header,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);`
But the server responds with a message telling me it didn't get the parameters I'm sending. When I send the same -F "data={}" from a terminal I get the correct response.
You shouldn't use Content-type: application/json. The value of the data parameter is JSON, but the parameters as a whole are sent in application/x-www-form-urlencoded format.
You also need to URL-encode the value of the data parameter.
The best solution is to use json_encode to create the JSON value, then use an array of post fields, then curl will encode the fields properly.
$header = array(
'Authorization: ' . $bearertoken
);
$fields = array('data' =>
json_encode(array('language' => 'EN',
'longitude' => $longitude,
'latitude' => $latitude)));
curl -F submits the form using Content-Type: multipart/form-data, following RFC 2388. You're specifically contravening that when you set the application/json content type.
$data = array('language' => 'EN', 'longitude' => $longitude, 'latitude' => $latitude);
$headers = array (
"Content-Type: multipart/form-data",
"Authorization: $bearertoken",
);
$postfields = array ("data" => json_encode($data));
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $options);
curl_exec($ch);
Also, if that's the actual documentation, it may be wrong. The format for Authorization header is Authorization: Bearer <token>.
Working on a CURL Post to a restful backend service. Using RESTClient extension in Firefox I get my intended response.
So I have my Authorization, Accept, and Content-Type Headers set in CURL
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
In RESTClient I have this JSON in the Body
{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
My question is: How to include that body in the PHP Curl request.
Pass it in postfields
jsonpoststring='{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
';
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
This should do the trick
$data = json_decode('{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}');
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec