2013-04-10 27 views
0

我必须传递一个数据数组到服务器,也得到服务器的响应..我得到未定义的变量在clientside.php上的错误我在哪里试图print_r接收..我如何得到响应在服务器端并发送回附加信息客户端..我现在用卷曲的函数来实现这个..如何获得附加到curl函数的数据在服务器端?

我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); 

我Serverside.php是这样

print_r($_POST['data']); 

我是gett荷兰国际集团下面的错误

*Notice: Undefined index: data* 
+0

尝试'print_r($ _ REQUEST);'并检查输出是什么? – 2013-04-10 07:48:00

+0

使用类似于萤火虫的东西来查看curl请求,看看它是否实际上以POST的形式发送这些值,甚至根本没有。 – Danny 2013-04-10 07:50:35

+0

错误消失了,但我没有得到数据..只有空阵列是我越来越... – 2013-04-10 07:52:31

回答

1

尝试http_build_query():

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

不要叫curl_exec两次:

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); 
+0

我在我的情况下使用$ abc上的http_build_query。 。但为了将一个数组分配给在服务器端有用的变量来获得响应,我有额外的额外$ post_data = array('data'=> serialize($ abc)); – 2013-04-10 07:54:38

+0

在你的情况下,你用数组调用CURLOPT_POSTFIELDS,使用http_build_query($ post_data)或设置正确的Content-Type – 2013-04-10 07:56:01

+0

因为你调用curl_exec两次,你什么也得不到。 – 2013-04-10 07:59:34

0

从PHP文档为curl_setopt,关于CURLOPT_POSTFIELDS选项:

如果值是一个阵列,所述Content-Type头将被设置为的multipart/form-data的

你必须建立一个有效的HTTP查询字符串(与http_build_query())或设置正确的内容类型,因为你使用数组作为值

0

尝试改变:

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

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

编辑:另外你可能要准备这样的回答: application/x-www-form-urlencoded or multipart/form-data?

EDIT2:请不要忘记安德烈说,大约除去第一curl_exec(),因为你不应该有它的两倍!所以删除:

if(curl_exec($ch) === false) { 
    echo 0; 
} else { 
    echo 1; 
} 
相关问题