thesubroot
thesubroot

Reputation: 448

using curl with php

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?

Upvotes: -1

Views: 141

Answers (3)

Debendra
Debendra

Reputation: 144

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

Upvotes: -1

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);

Upvotes: 2

hek2mgl
hek2mgl

Reputation: 158280

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.

Upvotes: -1

Related Questions