Why isn't my payload sent with this POST request? - php

I'm trying send a POST request to my API, with a payload ($data).
$api = new CoindRPC();
$txninfo = $api->gettransaction($argv[1]);
$txinforaw = $api->getrawtransaction($txninfo['txid']);
error_log('=== WALLETNOTIFY ===');
error_log('txninfo: '. print_r($txninfo,true));
$page_containing_sender = file_get_contents('http://redacted.com/api/getrawtransaction?txid=' . $txninfo['txid'] . '&decrypt=1');
$sender_parent_obj = json_decode($page_containing_sender, true);
$possible_senders = $sender_parent_obj['vout'][0]['scriptPubKey']['addresses'];
$encoded_possible_senders = json_encode($possible_senders);
echo json_encode($possible_senders);
$url = 'https://redacted.com/api/register_domain_name';
foreach($txninfo['details'] as $id => $details) {
$data = array(
'txid' => $txninfo['txid'],
'tot_amt' => $txninfo['amount'],
'tot_fee' => $txninfo['fee'],
'confirmations'=> $txninfo['confirmations'],
'comment' => $txninfo['comment'],
'blocktime'=> $txninfo['blocktime'] ? $txninfo['blocktime']:$txninfo['time'],
'account' => $details['account'],
'address' => $details['address'],
'category' => $details['category'],
'amount' => $details['amount'],
'fee' => $details['fee'],
'possible_senders' => $encoded_possible_senders,
);
}
$options = array(
'http' => array(
'header'=> array(
'WWW-Authenticate: Token',
'Authorization: Token [redacted]',
'Accept: application/json',
'Content-type: application/json'
),
'method'=> 'POST',
'content'=> json_encode($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
At my API, I get a 500 because, whenever I try to retrieve the POST data, I see that there is none. Does anyone know what's wrong? I'd really appreciate some help. Thanks in advance for any and all help.
What I've tried: changing json_encode to http_build_query, with changing Accept to the appropriate type for http_build_query, and Content-type to the appropriate type for http_build_query.

Related

PHP post request with file_get_content

Please someone to help me
I'm trying to POST a request with file_get_contents and I receive a 415 code error message. I'm not a pro dev and I want to know what is wrong in my source code. Here is my problem:
I generate a token which is a parameter of the second request
the generated token must be used for the POST request
I receive a 415 Error code message. I don't know how to correct that source
$tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxx/password/xxxx');
$toke =json_decode($tok, true);
$token=$toke['token'];
$data = json_encode(
array(
'from' => 'TEST',
'to' => '335546821546',
'type'=> 'Text',
'content'=> 'Test',
'sendDateTime'=> '2020/07/07'
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => "Authorization Token " . base64_encode("$token"),
'content' => $data
)
);
$context = stream_context_create($options);
$url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';
$result = file_get_contents($url,false,$context);
If you are getting token and facing issue in post call only then i have noticed that you have missed a few Request Headers:
like Content-type, Content-length, I have check with the different URLs, It is working for me try it
<?php
$tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxx/password/xxxx');
$toke =json_decode($tok, true);
$token=$toke['token'];
$data = json_encode(
array(
'from' => 'TEST',
'to' => '335546821546',
'type'=> 'Text',
'content'=> 'Test',
'sendDateTime'=> '2020/07/07'
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Content-length: " . strlen($data) . "\r\n" .
"Authorization Token: " . base64_encode("$token") . "\r\n",
'content' => $data
)
);
$context = stream_context_create($options);
$url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';
$result = file_get_contents($url,false,$context);
var_dump($result);

PHP sends GET instead of POST

I have to post some data, but the same adress have some GET and POST functions. My PHP is sending a GET instead of a POST.
$apiURL = 'https://myAPI.com.br/api';
$data = http_build_query(array('postdata' => 10));
$uriRequest = $apiURL.'/main';
$options = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
'https' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $data
),
);
$context = stream_context_create($options);
$result = file_get_contents($uriRequest, false, $context);
if ($result === FALSE) {
return var_dump($result);
}
return var_dump($result);
I know the ssl part it isnt safe, but it is just for prototyping purpose.
I cant get PHP to POST intestead of GET on the adress 'https://myAPI.com.br/api/main'.
Judging from http://php.net/manual/de/function.stream-context-create.php#74795 the correct way to create a stream context for a https secured page is:
<?php
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
As you can see we are using 'http' => array... instead of https.

Sending a post request with parameters in php to get Json data

I'm trying to get some json data from another server that needs 3 parameters so that I can access it. I saw this code in internet but its not working. I have searched a lot and I haven't found a solution. When I run this code I get :
use pass and code are missing
(is the response from the server I'm requesting), I hope I made things clear if there's anything I didn't explain please do tell.
$url = 'http://xxxx/.js';
$data =http_build_query(array('user' => 'xx',
'pass' =>'xxx ',
'code' =>'xxx')
);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $data
)
);
$context= stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { }
echo $result ;
Try to change your payload to json...
$data = json_encode( array(
'user' => 'xxx',
'pass' => 'xxx',
'code' => 'xxx'
) );
and to remove \r\n from your content type
"Content-type: application/json"

HTTP request failed Length required

I humbly come before the people for some much needed assistance with this..
I am using (or trying to use) the skyscanner API - http://partners.api.skyscanner.net/apiservices/pricing/v1.0 as documented here. But I am coming up against this error:
HTTP request failed! HTTP/1.1 411 Length Required
PHP attempt 1
function getSkyScanner() {
$url = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0?apiKey=MY-API-KEY';
$headers = array( 'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/xml');
$contextData = array (
'method' => 'POST',
'header' => $headers);
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo 'error'; }
var_dump($result);
}
My server doesn't support cURL so I'm in need of a solution without it. I'm using localhost with WAMP but I have also tried a live version which comes up with the same error and seemingly same problem. I have tried almost every combination of variables in order to correct the error with a discouraging amount of success (none). This is one such variation including the form contents that I am attempting to send.
PHP attempt 2
function getSkyScanner() {
$url = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0?apiKey=MY-API-KEY';
$params = array( 'country' => 'GB',
'currency' => 'GBP',
'locale' => 'en-GB',
'originplace' => 'LHR',
'destinationplace' => 'EDI',
'outbounddate' => '2016-10-10',
'adults' => '1'
);
$query = http_build_query($params);
$headers = array( 'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/xml');
$contextData = array (
'method' => 'POST',
'header' => $headers,
'content' => $query );
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo 'error'; }
var_dump($result);
}
This throws out:
Content-type not specified
Followed by:
HTTP/1.1 400 Bad Request
If you can help I would appreciate some insight right about now.
Many thanks!
You create headers in a wrong way - instead of an array, headers should be passed as a string.
$headers = "Content-Type: application/x-www-form-urlencoded\r\n" .
"Accept: application/xml\r\n";
You can use below snippet to create the correct request:
function prepare_headers($headers) {
return
implode('', array_map(function($key, $value) {
return "$key: $value\r\n";
}, array_keys($headers), array_values($headers))
);
}
function http_post($url, $data, $ignore_errors = false) {
$data_query = http_build_query($data);
$data_len = strlen($data_query);
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/xml',
'Content-Length' => $data_len
);
$response =
file_get_contents($url, false, stream_context_create(
array('http' => array(
'method' => 'POST',
'header' => prepare_headers($headers),
'content' => $data_query,
'ignore_errors' => $ignore_errors
)
)
));
return (false === $response) ? false :
array(
'headers' => $http_response_header,
'body' => $response
);
}
Example usage of http_post method:
$result = http_post('http://business.skyscanner.net/apiservices/pricing/v1.0', array(
'apiKey' => 'YOUR_API_KEY',
'country' => 'UK',
'currency' => 'GBP',
'locale' => 'en-GB',
'locationSchema' => 'iata',
'originplace' => 'EDI',
'destinationplace' => 'LHR',
'outbounddate' => '2016-10-10',
'adults' => '1'
), false);
Parameter $ignore_errors in http_post method is reponsible for fetching the content even on failure status codes (400, 500, etc.). If you receive Bad request, set ignore_errors = true -> you'll receive full response from server.

How to send a gcm message in php without using cURL?

I'm using the following code to make HTTP POST request to the gcm server.
The code always returns "Unauthorized Error 401". I read that it is about the headers but can't figure out whats wrong.
Can anyone tell me what's wrong?
Is there any other way to send a gcm message?
Any help would be appreciated.
$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";
$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));
$headers = array('Authorization: key=' . $api_key,'Content-Type: application/json');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Tested your code with my API key. I am able to send request to GCM and getting proper response. It seems like your API Key is having the problem
<?php
$msg_url = "http://msg.com";
$msg_title = "message_title";
$ids = array('reg_id_1', 'reg_id_2');
$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";
$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));
$headers = array('Authorization: key=' . $api_key,'Content-Type: application/json');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>

Categories