PHP CURL functions for POST and GET request using Codeigniter

Install the curl library from https://github.com/philsturgeon/codeigniter-curl
CodeIgniter-cURL is a CodeIgniter library which makes it easy to do simple cURL requests and makes more complicated cURL requests easier too.
  //load the Curl library
  $this->load->library('Curl');        

  //Request using POST Method
   $url = 'http://postexample.com/json.php';       
   echo $this->curl->simple_post($url, false, array(CURLOPT_USERAGENT => true));

   //Request using GET Method
   $get_url = http://getexample.com/json.php;  
   echo $this->curl->simple_get($get_url, false, array(CURLOPT_USERAGENT => true)); 
It is very important to set CURLOPT_USERAGENT, otherwise many servers may refuse your request if you don't pass user agent.
   //PHP version, you can put anyvalue for CURLOPT_USERAGENT, but it shouldn't be empty or blank.
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
I tried without setting CURLOPT_USERAGENT in GOOGLE CHROME, it didn't worked. I spent more than 4 hours to find a solution, why it is not returning the result. Finally, I found this post http://www.silverphp.com/php-curl-function-for-post-and-get-request.html and try with my luck. And it worked. :)

Comments

  1. I guess you for got to change the comment on get method

    ReplyDelete
  2. hi,
    I get this message Call to a member function library() on null
    when i call $this->load->library('Curl');

    ReplyDelete
  3. function fetch_user(){
    $this->load->library('curl');
    $this->curl->create('https://www.formget.com/');

    }



    Fatal error: Call to undefined function curl_init() in F:\xampp\htdocs\rest\application\libraries\Curl.php on line 282
    A PHP Error was encountered

    Severity: Error

    Message: Call to undefined function curl_init()

    Filename: libraries/Curl.php

    Line Number: 282

    Backtrace:

    ReplyDelete
  4. Looking for top-notch Laravel development services? Look no further than Connect Infosoft Technologies. We are a leading software development company with a strong expertise in Laravel framework. Our team of experienced developers excels in delivering high-quality and scalable web applications using Laravel. Looking for Laravel Dev Team in India

    ReplyDelete

Post a Comment

Popular Posts