Viren Rajput
Viren Rajput

Reputation: 5766

PHP Script not able to make Http request

I have writtten this script in php to make a http request to http://ubaid.tk/sms/sms.aspx

Here's the script-

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

And I need this script to send a http request as http://ubaid.tk/sms/sms.aspx/uid=814974&pwd=pass&msg=$_REQUEST['message']&phone=$_REQUEST['mobileno.']&provider=way2sms

The variables replaced and get back the response which the request gets and print it as it is. I have modified this script along with the code because I m still not able to get the correct output with it.

I need to convert it to POST request what more modifications do I need to do?

Upvotes: 1

Views: 555

Answers (2)

Lars
Lars

Reputation: 600

This should do the trick... but you should also add in some sanitization on your inputs to help protect against the possibility of injection (this is an entirely different discussion).

Sending Via 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>';
?>

Sending Via 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>';
?>

Upvotes: 2

Gustav Bertram
Gustav Bertram

Reputation: 14905

You were not escaping the string properly:

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

Upvotes: 1

Related Questions