以字符串格式获取卷曲响应

时间:2012-04-25 07:08:13

标签: php curl

我正在尝试执行网址并获得响应。以下是执行curl的代码。我希望curl执行在$ result中返回一个字符串。

<?php
$fields = array
(
'username'=>urlencode($username),
'pwrd'=>urlencode($pwrd),
'customer_num'=>urlencode($customer_num)
);

$url = 'http://localhost/test200.php';
//open connection
set_time_limit(20);
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
echo $result; //I want $result to be "Successful"
?>

这是我在localhost上的test200.php:

<?php
$usernam = $_POST['username'];
$pass = $_POST['pwrd'];
$customer_num = $_POST['customer_num'];
echo "Successful!";
?>

我在test200.php中做了哪些更改?请帮忙。

3 个答案:

答案 0 :(得分:2)

您应该使用curl执行返回的httpcode,而不是依赖于返回的字符串

$res = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

此处 - http://www.webmasterworld.com/forum88/12492.htm

答案 1 :(得分:1)

将数据发送到 test200.php 后,执行相应的操作,例如将已发布的值插入表格并成功

echo "Successful!";

或在test200.php中打印相同的...假设您在test200.php代码中执行插入代码就像

<?php
$qry = "INSERT INTO `your_table` (`field_customer_name`, `field_username`, `field_password`) VALUES ($fields['customer_num'], $fields['username'], some_encrypt_fxn($fields['pwrd']))";
mysql_query($qry);
$err_flag = mysql_error($your_conn_link);
if($err_flag == '') {
  echo "Successful!";
}
else {
  echo "Failed, Error " . $err_flag;
}
?>

如果获得“成功”的目的!是检查cURL是否返回成功然后我建议使用Prateik使用返回状态代码的答案

答案 2 :(得分:0)

以某种方式简单的打印(“成功”); test200.php中的声明效果很好。我现在得到的响应如下:HTTP代码:0数组([0] =&gt; [1] =&gt;成功)