I want to pass an array or a variable using cURL and post to a remote server and echo the contents of the array or value of the variable. Seems simple enough. I have been scouring the internets in search of a solution and I’m 90% sure the problem lies somewhere between my keyboard and my chair so I’m here to post code,which is a mess due to the troubleshooting code added but is still simple enough to figure out easily.
To the code; this is what is on my server and sending to the remote server.
[php]/*
- Data which is to submitted to the remote URL
*$_POST[‘key’] = “foo”;
*/
$post_array = array(“mahesh”,“chari”);
/*
- Initialize cURL and connect to the remote URL
- You will need to replace the URL with your own server’s URL
- or wherever you uploaded this script to.
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://www.5thandpenn.com/Worms/curlpost_hanlder.php’ );
/*
- Instruct cURL to do a regular HTTP POST
*/
curl_setopt($ch, CURLOPT_POST, TRUE);
/*
- Specify the data which is to be posted
*/
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
/*
- Tell curl_exec to return the response output as a string
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
/**
- Execute the cURL session
*/
$response = curl_exec($ch );
/**
- Close cURL session and file
*/
curl_close($ch );
echo $response; // is Welcome Maheshchari.[/php]
The response I get is:
Welcome website string(6) “mahesh” string(5) “chari” string(0) “” string(0) “” NULL NULL
The code on the remote server is this:
[php]$site='website ';
$firstname = ‘text’;
$firstname = isset($_POST[‘fname’]) ? $_POST[‘fname’] : ‘’;
$lastname= isset($_POST[‘lname’]) ? $_POST[‘lname’] : ‘’;
$array = array($_POST[0], $_POST[1]);
// $firstname=$_POST[0];
// $lastname=$_POST[1];
echo "Welcome ";
echo $firstname;
echo $lastname;
echo $site;
print $firstname;
var_dump($array[0]);
var_dump($array[1]);
var_dump($firstname);
var_dump($lastname);
var_dump($post_array);
var_dump($post_array[‘fname’]);
echo htmlspecialchars($_POST[“fname”]);
[/php]
The response from the remote server is:
Welcome website NULL NULL string(0) “” string(0) “” NULL NULL
Remote server php version: 5.6.17 on Linux
Php version on my server is: 5.6.10 on MAMP
Why is the remote server receiving NULL and empty array but echoing correctly and most importantly how can I fix this to work?