php receiving null values and empty array

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?

Well, I have two servers that send info back and fourth in the form of arrays. On the sending server, it takes
the data and places it into an array. Then, on the other end, I just “unserialize” it and it results in a perfect as-was array. I never seem to have an issue. If the sending server sends data as standard text AND arrays, you
might have to test it to know if it is an array or not. It is much better if you know the format of the data coming
in. In my experience, I used two variables to send from server to server. The first variable or argument tells the
other end what data is being sent. Then, the second argument contains the data. In this manner, the receiving
server can test the first argument and then handle the second as needed. This seems to work well… Here is one
way you can do this. In the CURL section on one server, I package up the data into an array like this:
[php]
// Set up connection to second server…
$url = “http://my-second-server.com/somefolder/hidden-file-to-handle-data.php”;

// Set up the data to be sent…
$fields = array(
“command” => “review”,
“arg1” => “1”,
“arg2” => $user_data,
);

// Build the urlencoded data
$postvars = http_build_query($fields);

// open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FAILONERROR,true);

// Execute the post
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

// Check if there’s error
if( !empty( $error ) || $result=="
") echo “
There was an error processing your request!
” . $error . “
”;

// Now create the results of what the remote site returns from processing…
$returned_data = unserialize(base64_decode($result));
[/php]
Basically, I send just about anything I need to this way. This end sends the CURL request to the other server
and the other server posts data in the form of a serialized array. Then, as you see above the data it returns
is turned back into an array that I called $returned_data. This data is an array of what ever you might need.
In this version I send to the second server three items, “command”, “arg1” and “arg2”. These are data that
is used in the second server to handle my requests. You can alter this data to be just about anything. If you
need to send an array instead of single arguments, just make sure you serialize the array when you set them.
In my example, I have a command that can be set to various controls that I use on the second server. Each
have different arguments which the second server uses to handle whatever command was sent…

Now the second server handles this CURL request as a simple POST’ed form would be handled. Therefore, I
take the data and handle it like this:
[php]
// Reset all of the variables to clear any of the previous posts that may have been sent…
$command = “”;
$arg1 = “”;
$arg2 = “”;

// First grab the three posted values from server one…
$command = $_POST[“command”];
$arg1 = $_POST[“arg1”]; // If encrypted or an array, you need to handle that here
$arg2 = $_POST[“arg2”];

// Send back the data from the database…
// Here I have a query (not shown) that uses the command and arguments to pull data from
// my second server’s database, process the data using the arguments and build an array to send back.
// For this example, I will create a dummied up one for you…
$user_data = array( “name” => “ErnieAlex”, “address” => “North Pole”, “more-data” => $some_variable, );

// Now, encode the array of data to send back…
$user_info = serialize($user_data);

// To send it back just echo it out…
echo $user_info;

// To send it with very loose protection, you can use base64 encoding. (Not very safe, but, helps in keeping
// it from prying eyes at first glance…
echo base64_encode(serialize($user_data));
[/php]
(Note: Do not use BOTH of the echo’s just one or the other. If you use the last one, you have to do the reverse at the other end like this: $data=base64_unencode(unserialize($results)); or similar…)

Well, if you handle it this way the second server can send data back and fourth between the servers and is
quite easy to do. You might want to add in some further security as the second server’s page is open to any
calls to it. I lock it up using IP checks and only let the first server in. Therefore no other callers will get info back.

Hope you understand this process…

Sponsor our Newsletter | Privacy Policy | Terms of Service