Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
how to using curl get api
  • Hello fellow Fuelers,
    Please. Help me using curl in fuelphp get api.

     use \Fuel\Core\Request;
     use \Fuel\Core\Format;
    class Controller_Test extends Controller_Rest
    {
        public function action_index()
        {
            $this->format = 'json';
            $curl = Request::forge('https://jsonplaceholder.typicode.com/posts', 'curl');
             $curl->set_method('get');
             $curl->execute();
            return ['test'=>$curl];
        }
    }

    Please demo.
    thanks you.
  • HarroHarro
    Accepted Answer
    It's a good start.

    After you have executed it, the result is in $curl->response(). This will return a standard Fuel response object.

    You can access it's payload using $curl->response()->body() (see http://fuelphp.com/docs/classes/response.html#/method_body).

    If you want to return the json you fetch as an array, use

        return \Format::forge($curl->response()->body(), 'json')->to_array();

    Note that this only works in REST controllers, standard controllers do not accept an array as return value.
  • Hi. I can't get api form url
    please help me
  • I don't understand what you mean... You can't get what?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion