PHP简单地发布到带有字段的URL,并显示来自服务器的答案


PHP Simple post to URL with fields and show the answer from server

我是php的新手,我想将一些数据发送到一个url并从服务器编写响应。无论我尝试什么,答案都是空的这是代码

$post_data['userid'] = "demo";
$post_data['password'] = "demo";
$post_data['to'] = "$to";
$post_data['message'] = iconv("UTF-8","Windows-1253","$text");
$post_data['from'] = 'sender';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('http://www.lexiconsoftware.gr/sms/warrior.asp');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curld, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curld, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curld, CURLOPT_COOKIEFILE, 'cookie.txt');
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);
$getresult = curl_get_contents($result);
echo $getresult;
$http_data = curl_exec($curld);
$curl_info = curl_getinfo($curld);
$headers = substr($http_data, 0, $curl_info['header_size']); //split out header
echo "Message Status: $http_data, headers: $headers<br><a href='"smser.php'">Send New</a>"; 
//close the connection
curl_close($curl_connection);

代码发送数据,但没有响应,只有一个空页面

1)首先使用$curld,但它不是由curl_init初始化的。请改用$curl_connection
2) 我没有看到任何curl_setopt($curl_connection, CURLOPT_POST, true);
3) 不要使用foreach和内爆()。对于您的数据可能包含&的情况,请将http_build_query()与以下内容一起使用:
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, http_build_query($post_data));4) 您将CURLOPT_RETURNTRANSFER设置为TRUE。没关系。所以请检查您的$result:
echo $result = curl_exec($curl_connection);
5) 旋度中不存在CCD_ 10。

首先:

foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

你做错了。传递的数据应该进行url编码。您只需使用CURLOPT_POSTFIELDS$post_data传递给curl_setopt,它就会为您创建请求体。

现在

$getresult = curl_get_contents($result);

什么是curl_get_contents?您正在使用curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);,因此$result将是您的响应主体。

现在,为什么存在第二个旋度连接$curld,它和你们的问题有什么关系?没有启动此连接的代码。