1

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?

5
  • Post here correct code (copy & paste) Commented Nov 11, 2011 at 12:07
  • @Virendra: are you facing any error?? Commented Nov 11, 2011 at 12:09
  • At first: you have POST method not GET. So the request will send to http://example/sms/sms.aspx without any params! At second: use sprintf() for creating string with vars inside, or enclosure variable with {} Commented Nov 11, 2011 at 12:10
  • TIP: write curl_setopt($ch, CURLOPT_NOPROGRESS, true); to debug Commented Nov 11, 2011 at 12:13
  • Do I need to do something for the cookies it is offering? Commented Nov 11, 2011 at 13:32

2 Answers 2

2

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>';
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Do I need to do something to handle the cookies
CURLOPT_COOKIEJAR CURLOPT_COOKIEFILE are used for this. Example(curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); & curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');) This should save the cookies into a file. Reference Article is electrictoolbox.com/php-curl-cookies
Hey Lars am not able to get the correct result using GET is it possible to do it with POST request?
I don't know why the POST method is still returing the wrong result! The request is'int being sent in the correct format using POST. I want the request to be sent in ubaid.tk/sms/… But somethings going wrong with it!
1

You were not escaping the string properly:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.