PHP Script не может сделать запрос Http - PullRequest
1 голос
/ 11 ноября 2011

Я написал этот скрипт в php, чтобы сделать http-запрос к http://ubaid.tk/sms/sms.aspx

Вот сценарий -

<code>    <?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 '
'; ?>

И мне нужен этот скрипт для отправки http-запроса как http://ubaid.tk/sms/sms.aspx/uid=814974&pwd=pass&msg=$_REQUEST['message']&phone=$_REQUEST['mobileno.']&provider=way2sms

Переменные заменили и вернули ответ, полученный запросом, и распечатали его как есть. Я изменил этот скрипт вместе с кодом, потому что я все еще не могу получить правильный вывод с ним.

Мне нужно преобразовать его в запрос POST, какие еще модификации мне нужно сделать?

Ответы [ 2 ]

2 голосов
/ 11 ноября 2011

Это должно сработать ... но вы также должны добавить некоторую дезинфекцию для ваших входов, чтобы защитить от возможности инъекции (это совершенно другое обсуждение).

Отправка через GET

<code><?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 '
';?>

Отправка по почте

<code><?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 '
';?>
1 голос
/ 11 ноября 2011

Вы неправильно экранировали строку:

curl_setopt($ch,CURLOPT_POSTFIELDS,"uid=814974&pwd=pass&msg=".$_REQUEST['message']."&phone=".$_REQUEST['mobileno.']."&provider=way2sms");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...