Using the following:
$auth = array(
'iAmWhiteListed' => 'me',
'otherStuff' => 'mystuff'
);
$sendAuth = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $auth ),
'header' => "Content-Type: application/json\r\n" . "Accept: application/json\r\n"
)
);
$authContext = stream_context_create( $sendAuth );
$authResult = file_get_contents( $url, false, $authContext );
How does a PHP script access the data send in the content of the http request? (In this case to verify the data therein and send back an appropriate response?)
When using Content-Type: application/json, the $_POST array is not populated. To fill it, I had to grab the input using php://input and decode it back into array format.
$data = file_get_contents("php://input");
$_POST = json_decode($data, true);
Related
Hello I'm trying to use the 1msg API for my company to send WhatsApp messages, but I get the following error:
I'm using the correct endpoint any of you have idea to correct this bug
<?php
// Define the endpoint URL
$url = "https://api.1msg.io/$channelId/sendMessage?token=$tokenId";
// Define the message data
$data = array(
"phone" => "$num",
'body' => array(
"text" => "this is a test message.",
)
);
// Use http_build_query to format the data as url encoded string
$postdata = http_build_query($data);
// Prepare the options for the request
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $postdata
)
);
// Create a stream context for the request
$context = stream_context_create($options);
// Send the request
$response = file_get_contents($url, false, $context);
> Print the response
print_r($response)
?>
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 trying to send a file_get_contents request to a URL with POST data and a cookie set.
My code is like that:
$postdata = http_build_query(
array(
'search' => 'test',
'token' => '0'
)
);
// Create a stream
$opts = array(
'http'=> array(
'method'=>"POST",
'content' => $postdata,
'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('https://example.com', false, $context);
echo $file;
This is posting the data as I can see, but the Cookie is not being sent during the request...
I also tried to use the same request but with CURL and I have the same problem with that.
Any help would be appreciated.
Thanks!
The headers may need to be separated by \r\n rather than just \n. You can also use an array, and they'll be sent properly.
'header' => array("Content-Type: application/x-www-form-urlencoded",
"Cookie: session_hash=123456789")
I'm testing an api using Postman but it doesnt send any data.
When I create a test using php function it works fine.
the api php
$json_str = file_get_contents("php://input");
$obj = json_decode($json_str, false);
the test php
$obj->CompanyCode= "39170";
$obj->CustomerNumber= "1800001";
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $obj ),
'header'=>array(
"Content-Type: application/json",
"Accept: application/json")
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "https://www.url.com/inquiry/", false, $context );
the postman
postman header
postman body
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