1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FuelPHPでcurlを使う

Posted at

pure PHPでもcurlは使えますが、せっかくFuelPHPを使っているのであればFuePHPにあるクラスを使ってみたいという感じです。

まずはpure PHPで。

curl.php
$curl = curl_init();//curlセッションを初期化
curl_setopt($curl, CURLOPT_POST, TRUE);//メソッドがPOSTの場合必要。GETなどならいらない
curl_setopt($curl, CURLOPT_URL, $url);//URLを指定
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);//POSTするデータ
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");//認証に必要なID、パスワード
$execute = curl_exec($curl);//実行
curl_close($curl);//curlセッションを閉じる

最低限ですがだいたいこんな感じになると思います。これをFuelPHPのRequest_Curlクラスで書いてみます。

request_curl.php
$curl = \Request::forge($url, 'curl')//URLを指定
->set_method('post')//メソッドを指定
->set_params($post)//POSTするデータ
->set_option(CURLOPT_USERPWD, $username. ':' .$password)//認証に必要なID、パスワード
->execute();//実行

だいぶ見やすくなった気が、しますかね?

ドキュメント
PHP→https://www.php.net/manual/ja/ref.curl.php
FuelPHP→http://fuelphp.jp/docs/1.7/classes/request/curl.html

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?