0

I'm new to php and i want to send some data to a url and write the respond from server. Whatever i try the response is empty Here is the code

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

The code sends the data but there is no response, just an empty page

4

2 回答 2

1

1)首先你使用$curld但它没有被curl_init初始化。改为使用$curl_connection
2) 我没有看到任何curl_setopt($curl_connection, CURLOPT_POST, true);
3) 不要使用 foreach 和 implode()。对于您的数据可能包含&使用http_build_query () 的情况:
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, http_build_query($post_data)); 4) 您设置CURLOPT_RETURNTRANSFERTRUE. 没关系。所以检查你的$result
echo $result = curl_exec($curl_connection);
5)curl_get_contents卷曲中没有。

于 2013-07-12T21:31:47.380 回答
0

Firstly:

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

You are doing it wrong. passed data should be urlencoded. You can just pass $post_data to curl_setopt with CURLOPT_POSTFIELDS and it will create request body for you.

Now

$getresult = curl_get_contents($result);

What is curl_get_contents? you are using curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); so $result will be your response body.

And now, why there is second curl connection $curld and how does it relate to your question? There is no code that initiates this connection.

于 2013-07-12T21:25:44.250 回答