0

I have to pass an array of data to server and also get response from server ..I am getting undefined variable error on clientside.php where i am trying to print_r the recieved ..How do i get the response at the server side and send it back to client side with additional information ..I am using curl function to achieve this ..

My clientside.php

 $url = "http://some_ip_address/../../../../serverside.php";
    //$abc is variable which contains all data in array format 
    $abc;
    $post_data = array('data' => serialize($abc)); 
    $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        if(curl_exec($ch) === false) {
        echo 0;
        } else {
        echo 1;
        }


$output= curl_exec($ch);
echo $output;
curl_close ($ch);

My Serverside.php goes like this

print_r($_POST['data']);

I am getting the following error

*Notice: Undefined index: data* 
Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61

3 Answers3

1

Try http_build_query():

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));

Do not call curl_exec twice:

client.php

$url = "http://some_ip_address/../../../../serverside.php";
//$abc is variable which contains all data in array format 
$abc;
$post_data = array('data' => serialize($abc)); 
$ch = curl_init();

curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$output= curl_exec($ch);
echo $output;
curl_close ($ch);

serverside.php:

 print_r($_REQUEST);
Andrey Volk
  • 3,513
  • 2
  • 17
  • 29
  • i have used http_build_query on $abc in my case..But in order to assign a array to variable which wud be useful at server side to get the response i have that extra that extra line of $post_data = array('data' => serialize($abc)); – Vaibs_Cool Apr 10 '13 at 07:54
  • In your case you call CURLOPT_POSTFIELDS with array, use http_build_query($post_data) or set the correct Content-Type – Andrey Volk Apr 10 '13 at 07:56
  • You get nothing because you call curl_exec twice. – Andrey Volk Apr 10 '13 at 07:59
  • i have done the above changes but no result ...Still i m getting an emoty array ..I m using print_r($REQUEST); Any changes in server side??? – Vaibs_Cool Apr 10 '13 at 08:13
  • underscore it print_r( $_REQUEST ); – Andrey Volk Apr 10 '13 at 08:16
  • No results Tried everything ...Now at this point my server side has print_r( $_REQUEST ); and my client side consists '$post_data = array("data=" . urlencode(serialize($abc))); curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post_data)); curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; charset=ISO- 8859-1'));' – Vaibs_Cool Apr 10 '13 at 09:43
  • lets check everything (firewall or something else). Does this work?: From the client "wget http://some_ip_address/../../../../serverside.php?test=1 -O server.txt" – Andrey Volk Apr 10 '13 at 09:51
  • Yeah @andrey It works ..In array i get 'Array ( [test] => 1 )' – Vaibs_Cool Apr 10 '13 at 09:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27933/discussion-between-andrey-volk-and-user1145009) – Andrey Volk Apr 10 '13 at 10:20
0

From the PHP doc for curl_setopt, regarding CURLOPT_POSTFIELDS option:

"If value is an array, the Content-Type header will be set to multipart/form-data."

You have to build a valid HTTP query string (with http_build_query()) or set the correct content-type, since you're using an array as a value

ulentini
  • 2,413
  • 1
  • 14
  • 26
0

Try changing:

$post_data = array('data' => serialize($abc));

into

$post_data = "data=" . urlencode(serialize($abc));

edit: Also you might want to ready this answer: application/x-www-form-urlencoded or multipart/form-data?

edit2: And please don't forget what Andrey said about removing the first curl_exec() because you shouldn't have it twice! So remove:

if(curl_exec($ch) === false) {
  echo 0;
} else {
  echo 1;
}
Community
  • 1
  • 1
nl-x
  • 11,762
  • 7
  • 33
  • 61