LoginSignup
5
6

More than 5 years have passed since last update.

PEARのHTTP_Request2を使ってみる

Posted at

CodeIgniterでAPI用のライブラリを作成するときにHTTP_Request2を使ったのでまとめてみた。

GETするだけ

サンプルコード

http_request2_sample.php

require_once 'HTTP/Request2.php';
$url = 'http://allabout.co.jp';
$http_request = new HTTP_Request2();
$http_request->setUrl($url);
$http_request->setMethod(HTTP_Request2::METHOD_GET);
$ret = $http_request->send();
var_dump($ret);

  • $http_request->setMethod でGET以外にもPOSTなども設定出来る。

パラメータがある場合

http_request2_sample2.php

require_once 'HTTP/Request2.php';
$url = 'http://allabout.co.jp';
$http_request = new HTTP_Request2();
$http_request->setUrl($url);
$http_request->setMethod(HTTP_Request2::METHOD_GET);
if(!empty($params)) {
    $param_url = $http_request->getUrl();
    $param_url->setQueryVariables(array(
             'package_name' => array('HTTP_Request2', 'Net_URL2')
                        ));
    $param_url->setQueryVariables($api_params);
    $http_request->setUrl($param_url);
}
$ret = $http_request->send();
var_dump($ret);

  • 一回getUrlでsetしたURLを取得
  • setQueryVariablesでパラメータをセット、
  • パラメータをセットしたURLをセットしなおして、send

あくまで上記はサンプルなのでパス通したほうがいいかもしれない。

5
6
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
5
6