Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
how to using curl get api
hoangquan94
January 2017
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.
Harro
January 2017
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.
hoangquan94
January 2017
Hi. I can't get api form url
please help me
Harro
January 2017
I don't understand what you mean... You can't get what?
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
January 2017
hoangquan94
January 2017