PHP脚本无法发出Http请求

时间:2011-11-11 12:01:26

标签: php curl

我已在php中编写此脚本以向http://ubaid.tk/sms/sms.aspx

发出http请求

这是脚本 -

    <?php
$connection_url = sprintf('http://ubaid.tk/sms/sms.aspx?uid=8149744569&pwd=passmsg=%s&phone=%s&provider=way2sms', $_REQUEST['message'], $_REQUEST['mobileno.']);
$ch = curl_init($connection_url);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Return the result
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
$data = curl_exec($ch); // Run the request
// Display the result    
echo "<pre>";
print_r($data); /* result of SMS API call*/
echo '</pre>';
?>

我需要此脚本发送http请求为http://ubaid.tk/sms/sms.aspx/uid=814974&pwd=pass&msg= $ _ REQUEST ['message']&amp; phone = $ _ REQUEST ['mobileno。']&amp; provider = way2sms

替换变量并获取请求获得的响应并按原样打印。我已经修改了这个脚本以及代码,因为我仍然无法使用它获得正确的输出。

我需要将其转换为POST请求我还需要做哪些修改?

2 个答案:

答案 0 :(得分:2)

这应该可以解决问题...但你还应该在输入上添加一些消毒剂,以防止注射的可能性(这是一个完全不同的讨论)。

通过GET发送

<?php
$connection_url = sprintf('http://example/sms/sms.aspx?uid=814974&pwd=pass&msg=%s&phone=%s&provider=way2sms', $_REQUEST['message'], $_REQUEST['mobileno.']);
$ch = curl_init($connection_url);
curl_setopt($ch, CURLOPT_HTTPGET, 1); // Make sure GET method it used
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Return the result
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
$data = curl_exec($ch); // Run the request
// Display the result    
echo "<pre>";
print_r($data); /* result of SMS API call*/
echo '</pre>';
?>

通过POST发送

<?php
// Setup Connection URL
$connection_url = sprintf('http://example/sms/sms.aspx');
// Setup Post Variables
$post_vars = sprintf('uid=814974&pwd=pass&msg=%s&phone=%s&provider=way2sms', $_REQUEST['message'], $_REQUEST['mobileno.']);
$ch = curl_init($connection_url);
curl_setopt($ch, CURLOPT_POST, 1); // Make sure POST method it used
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_vars); // Attach post variables
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Return the result
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
$data = curl_exec($ch); // Run the request
// Display the result    
echo "<pre>";
print_r($data); /* result of SMS API call*/
echo '</pre>';
?>

答案 1 :(得分:1)

你没有正确地转义字符串:

curl_setopt($ch,CURLOPT_POSTFIELDS,"uid=814974&pwd=pass&msg=".$_REQUEST['message']."&phone=".$_REQUEST['mobileno.']."&provider=way2sms");