How to GET sent json POST cURL in PHP - php

I'm trying to access data that has been sent from sender.php to receiver.php using post.
Here is my sender.php
<?php
$param = array(
'Name' => 'Mark',
'Age' => '28'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://example/receiver.php',
CURLOPT_POST => 'TRUE',
CURLOPT_HEADER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: '.strlen(json_encode($param))
),
CURLOPT_POSTFIELDS => json_encode($param),
)
);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
And here's my receiver.php.
I need help on how I can display the data entered as result
<?php
echo json_decode(file_get_contents('https://example/sender.php'));
?>

Related

How to correctly output specific cURL response from POST request

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));

add basic authorization to php curl script

How do I add basic authorization to my below php script? I am trying to send data to an API
$ch = curl_init();
$token = 'eyJpZCI6MTExOCwiZW52IXXXXXXXXJzaWQiOjYsImFsZyI6IkhTMjU2In0.eyJzdWIiOiJhZG1pbkBtaW5leDXXXXXXXTAsImV4cCI6MTU0ODk0ODgxMH0.owTm-ItzXnpqVSFbXXXXXXXXXX';
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
$curlConfig = array(
CURLOPT_URL => "https://api.endpoint.com",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'From' => 'HI',
'To' => '024XXXXXXX',
'Content' => 'hello there'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result
I guess this will work (I'm not sure, as I never used Authorization)...
$ch = curl_init();
$token = 'eyJpZCI6MTExOCwiZW52IXXXXXXXXJzaWQiOjYsImFsZyI6IkhTMjU2In0.eyJzdWIiOiJhZG1pbkBtaW5leDXXXXXXXTAsImV4cCI6MTU0ODk0ODgxMH0.owTm-ItzXnpqVSFbXXXXXXXXXX';
$curlConfig = array(
CURLOPT_URL => "https://api.endpoint.com",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'From' => 'HI',
'To' => '024XXXXXXX',
'Content' => 'hello there'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer ".$token
));
curl_close($ch);
echo $result
That's it?

PHP Box API revoke token : Client id was not found in the headers or body

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);

Send HTTP post request to ASP.Net Web API using PHP

<html>
<body><input type="button" id="send" value="Send"></body>
</html>
There'll be a button in my html,
When it's clicked, i'd like to connect to http://localhost:81/Help/Api/POST-Login
which is my api.
Then i'd like to post data to it.
Pretty confused now, any ideas guys. Thanks in advance
<?php
$url = "http://localhost:81/Help/Api/POST-Login";
$data = array(
'message' => 'hi,
mobile' => 12345678,
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
Use the below code to get the response from .NET API:
$url = "http://localhost:81/Help/Api/POST-Login";
$data = array( 'message' => 'hi, mobile' => 12345678,
);
$options = array(
CURLOPT_CUSTOMREQUEST => "POST", //set request type post or get
CURLOPT_POST => false, //set to GET
CURLOPT_COOKIEFILE => "cookie.txt", //set cookie file
CURLOPT_COOKIEJAR => "cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
echo $header['content'];

curl -F "data={json:'code'}" in PHP?

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>.

Categories