Use curl with FuelPHP

Aug 28, 2020 PHP curl FuelPHP api

You can use curl with pure PHP, but if you are using FuelPHP, I would like to use the classes in FuePHP.

First with pure PHP.

$curl = curl_init(); //initialize curl session
curl_setopt($curl, CURLOPT_POST, TRUE); // Required if method is POST. No need for GET
curl_setopt($curl, CURLOPT_URL, $url); //specify URL
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); //POST data
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); //ID and password required for authentication
$execute = curl_exec($curl); //execute
curl_close($curl); //close curl session

At a minimum, I think it will look something like this. Let’s write this in the FuelPHP Request_Curl class.

$curl = \Request::forge($url,'curl') //specify URL
- >set_method('post') //specify method
- >set_params($post) //Data to POST
- >set_option(CURLOPT_USERPWD, $username.':' .$password) //ID and password required for authentication
- >execute(); //Execute

Do you feel that it is easier to see?

document PHP → https://www.php.net/manual/ja/ref.curl.php FuelPHP → http://fuelphp.jp/docs/1.7/classes/request/curl.html