我需要将我的表单中的一些数据以html格式发送到webservice。 (为此,我需要进行POST操作)
我已经看到我可以使用php cURL传输信息的研究。但是在所有的例子中,我都没有查看将数据发送到webservice,只查看打印$ _POST变量的文件。
我有这个网络服务:http://192.168.1.1/fastfood/event/attendee(示例) 我尝试以数组形式发送数据。
例如,我尝试发送:attendee = array( 'name' => $_POST['name'] , 'lastname' => $_POST['lastname'] , 'address' => $_POST['address'] );
然后,Web服务取出数组数据。 ¿怎么做?
更新1:
这是我现在正在做的代码......但是不行:(
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$attendee = array(
'name' => "$name",
'lastname' => "$lastname",
'address' => "$address"
);
$url_target = 'http://192.168.1.1/fastfood/event/attendee';
//$header = array('Content type: multipart/form-data');
$user = 'root';
$pass = '123';
$userpasswd = "$user:$pass";
$ch = curl_init($url_target);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $userpasswd);
//curl_setopt($ch, CURLOPT_URL, $url_target);
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
$getInfo = curl_getinfo($ch);
curl_close($ch);
变量$ result返回FALSE,变量$ getInfo返回http_code = 500,Content-Type = Null。
当我发送类似数组的数据时,阅读cURL的文档,内容类型应该是“multipart / form-data”,但是,也不适用于我。
答案 0 :(得分:2)
// Here is the data we will be sending to the service
$data = array(
'name' => $_POST['name'],
'lastname' => $_POST['last_name'],
'address' => $_POST['address']
);
$curl = curl_init('http://192.168.1.1/fastfood/event/attendee');
curl_setopt($curl, CURLOPT_POST, 1); //Choosing the POST method
curl_setopt($curl, CURLOPT_URL, 'http://localhost/helloservice.php'); // Set the url path we want to call
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Make it so the data coming back is put into a string
curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data); // Insert the data
// Send the request
$result = curl_exec($curl);
// Free up the resources $curl is using
curl_close($curl);
echo $result;
Chad Lung撰写GiantFlyingSaucer