I am trying to send $_POST data to my server using cURL, and I am following the advice on this website https://www.delftstack.com/howto/php/php-post-request/
however when I dump the contents nothing is being saved to the array.
my code is as follows
test-api-key.php
<pre>
<?php
$data = [
"firstName" => "Rob",
"lastName" => "Test",
];
function submit($data) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost/testserver/request.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
echo $response;
}
submit($data);
echo "var_dump". "<br>";
var_dump($_POST);
printf("$_POST");
?>
</pre>
and the receiving file, request.php, is:
<?php
echo "test-echo <br>";
echo "name: ". $_POST['firstName']. "<br>";
echo "name: ". $_POST['lastName']. "<br>";
echo $_POST[$data];
printf("$_POST");
?>
The output from both files when the test-api-key.php is loaded, is:
test-api-key.php:
test-echo
name: Rob
name: Test
Array
var_dump
array(0) {
}
Array
and request.php output is:
test-echo
name:
name:
Array
As can be seen with the dump, nothing is being stored within the $_POST variable. Both the test-api-key.php and the request.php are within the same localhost directory, however when I implement it, with live data, it will be being sent from one server to another. I understand I will need to include a header array variable when using the live data, and I have the details for that from the live api server.
I have only included both var_dump() and printf() in order to try and get as much information about what is being held within the $_POST variable as I can for debugging.
I'm trying to understand how this all works, before implementing it with live data, hence why I am just setting up a few test page with my localhost.
source https://stackoverflow.com/questions/72671609/post-super-variable-dump-only-contains-an-empty-array
No comments:
Post a Comment