PHP脚本无法发出Http请求
我已经用php编写了这个脚本,向 这是剧本-PHP脚本无法发出Http请求,php,curl,Php,Curl,我已经用php编写了这个脚本,向 这是剧本- <?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_se
<?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>';
?>
我需要这个脚本以$_request['message']和phone=$_request['mobileno.]&provider=way2sms的形式发送http请求
变量被替换并返回请求得到的响应,并按原样打印。我已经修改了这个脚本和代码,因为我仍然无法获得正确的输出
我需要将其转换为POST请求我还需要做哪些修改?您没有正确转义字符串:
<?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>';
?>
这应该可以解决问题。。。但你也应该在你的输入中加入一些消毒措施,以防止注射的可能性(这是一个完全不同的讨论) 通过GET发送
<?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>';
?>
邮寄
Post here correct code(copy&paste)@Virendra:您是否面临任何错误??首先:您有Post
方法而不是GET
。因此请求将发送到http://example/sms/sms.aspx
没有任何参数!第二步:使用sprintf()
创建内部带有变量的字符串,或使用{}
创建外壳变量提示:编写curl\u setopt($ch,CURLOPT\u NOPROGRESS,true)代码>要调试,我需要为它提供的cookies做些什么吗?我需要做些什么来处理cookiesCURLOPT_COOKIEJAR CURLOPT_Cookie文件用于此。例如(curl_setopt($ch,CURLOPT_COOKIEJAR,'/tmp/cookies.txt');&curl_setopt($ch,CURLOPT_COOKIEFILE,'/tmp/cookies.txt'))这应该将cookies保存到一个文件中。参考文章Hey Lars我无法使用get获得正确的结果是否可以使用POST请求进行此操作?我不知道为什么POST方法仍然返回错误的结果!请求正在使用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>';
?>