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);
Related
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.
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.
I am using recaptcha 2 and getting this weird error:
Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in ... on line 38.
the code is:
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<?php
$page="contact";
include("includes/navbar.php");
echo '<div id="wrapper">';
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '6LfJnWQUAAAAANMRQApsnetVjggDqnn4hx7Ltbyz',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<h1>You did not prove you are human.</h1><h2> Please go back and complete the form!</h2>";
exit();
}
else if ($captcha_success->success==true) {
more code here to execute if captcha successfull
line 38 that triggers the error message is:
$verify = file_get_contents($url, false, $context);
The error message appears whether or not the robot box has been ticked. If the box has not been ticked, the "You did not prove you are human" message appears and if the robot box has been ticked the code is correctly processed, although the error message still appears.
How can I remove the error message? The site has an SSL certificate so I tried changing :
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
to:
$options = array(
'https' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
and that removes the error message, but then the "You did not prove you are human" message appears, even if the robot box is checked.
I am stumped.
Regards
Tog
Yes, I fixed it with:
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<?php
$page="contact";
include("includes/navbar.php");
echo '<div id="wrapper">';
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '6LfJnWQUAAAAANMRQApsnetVjggDqnn4hx7Ltbyz',
'response' => $_POST["g-recaptcha-response"]
);
$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);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<h1>You did not prove you are human.</h1><h2> Please go back and complete the form!</h2>";
exit();
}
else if ($captcha_success->success==true) {
more code here to execute if captcha successfull
I think there is a parameter missing inside your options, try something like this:
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data),
'header' => 'Content-Type: application/x-www-form-urlencoded'
)
);
$secretKey = '------------Your S-Key---------------';
$token = $_POST["g-token"];
$ip = $_SERVER['REMOTE_ADDR'];
/* ======================= POST METHOD =====================*/
$url = "https://www.google.com/recaptcha/api/siteverify?";
$data = array('secret' => $secretKey, 'response' => $token, 'remoteip'=> $ip);
// use key 'http' even if you send the request to https://...
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header' => 'Content-Type: application/x-www-form-urlencoded'
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
if($response->success)
{
echo '<center><h1>Validation Success!</h1></center>';
}
else
{
echo '<center><h1>Captcha Validation Failed..!</h1></center>';
}
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);
?>
I tried to send a post request using this PHP code but throw me 401 Unauthorized error:
$username = 'MyDomain\testuser';
$password = '123456';
$url = 'http://10.20.30.40:8080/TargetPage.aspx';
$data = array(
'username' => $username,
'password' => $password,
'postdata' => 'InputParameter1=Test1&InputParameter2=Test2'
);
$options = array(
'http' => array
(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
You're not doing proper basic auth. You have to build the authenticate header yourself:
$options = array(
'http' => array(
'header' => 'Authorization: Basic ' . base64_encode("$username:$password")
)
);
You can't just shove a username/password elements into the array and expect it to work. PHP doesn't know you're building a stream to HTTP basic auth... you have to provide EVERYTHING yourself.