PHP sends GET instead of POST - php

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.

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

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

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.

Not working Using file_get_contents to POST data

I try to get content from https://www.freeformatter.com. I want to get POST data from that site. but my PHP script not working.
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$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
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://www.freeformatter.com/html-formatter.html', false, $context);
var_dump($result);
This will not worked. Reason is freeformatter.com is not allowing to give API or POST call. They always return you view-source of html page. This is allow to use from UI but not by API call. I tried with CURL post also. its gives return back view-source of html.
Here is code you can try this works well with different
<?php
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data['htmlString']) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://en99otyaenia.x.pipedream.net', false, $context);
var_dump($result);
Code doesnt have any issue. Issue is with URL.

PHP - Content-type not specified assuming application/x-www-form-urlencoded

For 2 days I'm having trouble with my PHP script on my server. I've changed nothing and suddenly it didn't work anymore.
Here is the code:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'method' => "POST",
'content' => $query,
),
);
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n",'method' => 'POST',
'content' => http_build_query($data),));
$contexts = stream_context_create($opts);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $contexts, -1, 40000);
I'm getting these error messages:
Notice: file_get_contents(): Content-type not specified assuming
application/x-www-form-urlencoded in
Warning: file_get_contents(https://mobile.dsbcontrol.de): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server
Error in
But when I try the script locally it works perfectly.
You are passing $contexts to file_get_contents() and that only contains the User-Agent header in the $opts array. All other headers and options are in the $options array which you add in to $context but aren't using. Try:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n".
"User-Agent:MyAgent/1.0\r\n",
'method' => "POST",
'content' => $query,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context, -1, 40000);
While the existing answers did not work for me, I managed to solve the problem like this:
The PHP Manual says params must be an associative array in the format $arr['parameter'] = $value. Refer to context parameters for a listing of standard stream parameters.
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($postdata)
);
$packet['method'] = "POST";
$packet['header'] = implode("\r\n", $header);
$packet['content'] = $postdata;
$transmit_data = array('http' => $packet);
$context = stream_context_create($transmit_data);
i'm using this
$url = '';
$result = json_decode(file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => http_build_query($dataQuery)
)
))), true);

Post data & show source code

I have question, is it possible to first post data to a site and then get the source code of the site I was posting the data to?
I tried with this:
$html = file_get_contents('http://test.com/test.php?test=test');
But the ?test=test is $_GET....
So yeah, I hope someone can help me! :) Thanks in advance (Sorry for bad english!).
You can use 3rd parameter of this function: context
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
// edit -little bug should be:
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Connection: close\r\n".
"Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'content' => $postdata
)
);
You can not get the source, but you can get the page/output as it is provided by the server. As you mention file_get_contents(), you can use it to send a POST-request, but it would look something like this.
// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://www.sample-post-page.com', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
Example from: http://fczaja.blogspot.se/2011/07/php-how-to-send-post-request-with.html

Categories