-1

I have an sms android app that works remotely using a http server, It need to get a formed url request like this :

http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456

When i type that url in the browser address bar and hit enter, the app sends the sms. I'm new to curl, and I dont know how to test it, here is my code so far:

  $phonenumber= '12321321321'
  $msgtext    = 'lorem ipsum'
  $pass       = '1234'

  $url = 'http://server.com:9090/sendsms?phone=' . urlencode($phonenumber) . '&text=' . urlencode($msgtext) . '&password=' . urlencode($pass);



  $curl = curl_init();
  curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url
  ));

So my questions are, is the code correct? and how to test it?

1
  • Why not run it to check what happens? Commented Aug 31, 2023 at 7:34

3 Answers 3

2

Altough this is a simple GET, I cannot fully agree with hek2mgl. There are many situations, when you have to take care of timeouts, http response codes, etc. and this is what cURL is for.

This is a basic setup:

$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($handler, CURLOPT_MAXREDIRS, 10); // optional
// curl_setopt($handler, CURLOPT_TIMEOUT, 10); // optional
$response = curl_exec($handler);
curl_close($handler);
Sign up to request clarification or add additional context in comments.

2 Comments

Using this, the url will be processed like it was accessed from the browser?
Yes, of course curl provides more functionality, but note that the extension may not being installed on some environments. Especially when it comes to shared hosting. But however +1 for the explanation ;)
-1

If you can access the url using the address bar in browser, then it is a HTTP GET request. The simplest thing to do that in PHP would be using file_get_contents() since it can operate on urls as well:

$url = 'http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456';
$response = file_get_contents($url);

if($response === FALSE) {
    die('error sending sms');
}

// ... check the response message or whatever
...

Of course you can use the curl extension, but for a simple GET request, file_get_contents() will be the simplest and most portable solution.

1 Comment

Thank you for you answer :) I'm going to test it as soon as I can.
-1

Follow this

<?php
    $phonenumber= '12321321321';
    $msgtext    = 'lorem ipsum';
    $pass       = '1234';
    $url = 'http://server.com:9090/sendsms?phone=' . urlencode($phonenumber) . '&text=' . urlencode($msgtext) . '&password=' . urlencode($pass);
    
    $crl = curl_init(); //Here we initialize a cURL
    curl_setopt($crl, CURLOPT_URL, $url);  //The URL to fetch. This can also be set when initializing a session with curl_init()
    curl_setopt($crl, CURLOPT_FRESH_CONNECT, true); //force the use of a new connection instead of a cached one
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); // Return page content
             
    $response = curl_exec($curl); //Perform cURL session and return response as string
    $err = curl_error($curl); //Grab errors(string) return for the current session
    
    curl_close($curl); // Destory or Close cURL session
    
    if ($err) {
        echo "Error #:" . $err;
    } else {
        echo $response;
    }                 
    

?>

For testing simple need to save this code xxx.php file and run it in php sever (xampp, WampServer, ..... so many other ways available)

Reference: https://www.php.net/manual/en/function.curl-setopt.php

12 Comments

Please add some explanation to your answer such that others can learn from it. What did you change, and why?
@NicoHaase explanation and reference added. Thanks for suggestion.
You still haven't explained whether the code is "correct" or how to test it
This is a 10 years old question with less than a hundred views. A new answer here hardly helps anyone. Also, FYI, http_build_query()
@YourCommonSense if not helpful, report this to stackoverflow to delete this question. Don't demotivate user to start post their answer. If I posted a wrong answer, let me know.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.