Vinay Kesharwani
Vinay Kesharwani

Reputation: 133

Codeigniter hit an url and get back

I am using codeigniter 3.0 in my webapp. I want to hit an URL then get back to a view. Lets say on clicking a button, I want to hit an API then return back to the same view with success message. Can anyone help?

My API is http://wmlab.in/api/getData.php?username=web

I tried file('http://wmlab.in/api/getData.php?username=web') but it is not working.

Upvotes: 2

Views: 1668

Answers (3)

Manoj Rana
Manoj Rana

Reputation: 3450

it should be work

$result=file_get_contents('http://wmlab.in/api/getData.php?username=web');

Upvotes: 0

Vinie
Vinie

Reputation: 2993

You can use file_get_contents('http://wmlab.in/api/getData.php?username=web') or can use curl;

For Curl use below code:-

$channel = curl_init();
curl_setopt( $channel, CURLOPT_URL, "http://wmlab.in/api/getData.php?username=web" );
curl_setopt( $channel, CURLOPT_RETURNTRANSFER, 1 ); 
$response= curl_exec ( $channel );
curl_close ( $channel );
// after curl response redirect to othe page
redirect('anypage');

For file get content use below code:-

$response=file_get_contents('http://wmlab.in/api/getData.php?username=web')
// after response redirect to othe page
redirect('anypage');

Hope it will help you.

Upvotes: 1

shankar kumar
shankar kumar

Reputation: 648

use curl to do that and also install curl if your server did not have curl.

     url="http://wmlab.in/api/getData.php?username=web";


     function getUrlContent($url){
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);

          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
          curl_setopt($ch, CURLOPT_TIMEOUT, 5);
          $data = curl_exec($ch);
          echo $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
          return ($httpcode>=200 && $httpcode<300) ? $data : false;
        }
       $content=getUrlContent($url);
        echo $content;

Upvotes: 1

Related Questions