Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: Consume RESTful service

Is there an easy to setup Codeigniter REST library which I can use to consume a RESTful service? I tried setting up this library. But could not set up Spark. Tried following steps:

  1. Adding a directory named sparks in the root of your codeigniter directory
  2. Adding a custom Loader Class to application/core/MY_Loader.php.

It gave me another error Cannot find spark path at sparks/curl/1.2.1/. Now I am stuck. Wondering why it is so hard to setup RESTful API in codeigniter.

Update: When I try to run

$this->load->spark('restclient/2.1.0');
        // Load the library
        $this->load->library('rest');
        // Run some setup
        $this->rest->initialize(array('server' => 'http://api.twitter.com/'));
        // Pull in an array of tweets
        $tweets = $this->rest->get('1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=jenksuy&count=2');
        $tweetobjects = json_decode($tweets);
        foreach ($tweetobjects['results'] as $tweet) {
            log_message('info', $tweet['text']);
        }

I am getting Error: Call to undefined method CI_Loader::spark()

like image 825
Himanshu Yadav Avatar asked Feb 06 '26 20:02

Himanshu Yadav


1 Answers

sparks is not necessary SEE EDIT. use this tutorial to start, the author also wrote the libraries

http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

download these two to work with the tutorial:

https://github.com/philsturgeon/codeigniter-restserver

https://github.com/philsturgeon/codeigniter-restclient

also at the end of the tutorial - theres many comments and questions, and the tutorial author has answered many of them

EDIT - whoops forgot you have to change one line. and you will need to DL the CI curl library. OK so in the rest client, in the file Rest.php starting at line 53

/* Not using Sparks? You bloody well should be.
    | If you are going to be a stick in the mud then do it the old fashioned way

    $this->_ci->load->library('curl');
    */

    // Load the cURL spark which this is dependant on
    $this->_ci->load->spark('curl/1.2.1');

so change it to load curl library the traditional way and commment out the spark reference

        $this->_ci->load->library('curl');


    // Load the cURL spark which this is dependant on
    // $this->_ci->load->spark('curl/1.2.1');
like image 68
cartalot Avatar answered Feb 09 '26 08:02

cartalot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!