12

I'm developing a web application for sending SMS to mobile from website like 160by2.

I can prepare the URL required for the HTTP GET request as mentioned in the API provided by the SMS Gateway provider smslane.com, here is the link for the API.

how to send the HTTP request from PHP?

I used cURL for that purpose but the response is not displayed. here is the code i used,

<?php 
$url="http://smslane.com/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898123456&sid=WebSMS&msg=Test message from SMSLane&fl=0";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );  
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
echo $content;
?>

fsockopen() was not used because port number unknown, mailed the support team for port number. (if any code for fsockopen through GET method is invited :). )

are there any other methods for doing this?

any help is welcome.

Thanks in advance.

EDIT

Could any one tell me is there any other way to send this HTTP Request except cURL and if possible give code for that.

I'm asking this because the current cURL code taking too much time for execution and fails after 60 seconds, i increased max_execution_time to 120 in php.ini on my local system even though it is not doing good for me :( .

srinivas
  • 360
  • 1
  • 5
  • 16
  • What's the URL? Please make sure you sanitise your username and password. You could be sending a malformed request by accident. – Shabbyrobe Aug 20 '10 at 06:28
  • @webdestroya i could accept answers if i got :) – srinivas Aug 20 '10 at 06:43
  • @Shabbyrobe updated the question please check. here i provided the dummy details but for actual details i can send SMS from the control panel the website given. – srinivas Aug 20 '10 at 06:46
  • I'm pretty sure I've found your problem, so I've reworked my answer. – Shabbyrobe Aug 20 '10 at 07:21
  • The Rook's answer can be used to try to reduce the execution time, but curl is almost certainly not your problem - it's going to be the remote server. Throw together some curl code that queries your localhost and you should see that it's instantaneous. Perhaps the server you're querying isn't that quick – Shabbyrobe Aug 21 '10 at 16:10

5 Answers5

12

Your problem is the way you are constructing the URL. The spaces you are including in the query string will result in a malformed request URL being sent.

Here is an example that replicates your circumstances:

request.php:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 
    'http://your_server/response.php?foo=yes we can&baz=foo bar'
);
$content = curl_exec($ch);
echo $content;

response.php:

<?php
print_r($_GET);

The output of request.php is:

Array
(
    [foo] => yes
)

The reason for this is the query string is not properly encoded and the server interpreting the request assumes the URL ends at the first space, which in this case is in the middle of the query: foo=yes we can&baz=foo bar.

You need to build your URL using http_build_query, which will take care of urlencoding your query string properly and generally makes the code look a lot more readable:

echo http_build_query(array(
    'user'=>'abc',
    'password'=>'xyz',
    'msisdn'=>'1234',
    'sid'=>'WebSMS',
    'msg'=>'Test message from SMSLane',
    'fl'=>0
));

You also need to set CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Shabbyrobe
  • 12,298
  • 15
  • 60
  • 87
8
<?php
print file_get_contents("http://some_server/some_file.php?some_args=true");
?>
rook
  • 66,304
  • 38
  • 162
  • 239
2

The below code will make a HTTP request with curl and return the response in $content.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,'http://www.anydomain.com/anyapi.php?a=parameters');
$content = curl_exec($ch);
echo $content;
?>
1

nothing wrong with your code that I can see right off. have you tried pasting the url into a browser so you can see the response? you could even use wget to download the response to a file. I suppose if you want to try fsocket that you would use port 80 as that's the default http port.

Al W
  • 7,539
  • 1
  • 19
  • 40
1

Try using following code.

$r = new HttpRequest('http://www.xencomsoftware.net/configurator/tracker/ip.php', HttpRequest::METH_GET);
try {
        $r->send();
         echo $r->getResponseCode(); 
         if ($r->getResponseCode() == 200) 
        {
        }
    }

make sure that you have loaded HttpRequest extension (share object0 in your server.

Elixir Techne
  • 1,848
  • 15
  • 20
  • how to load the HTTPRequest extension on my system/server ? i tried that method but given error for extension not loaded. i could not figure out how to install that extension. – srinivas Aug 20 '10 at 09:02