facebook twitter hatena line email

Php/zend framework/zend http client

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サンプル(GET

require_once 'Zend/Http/Client.php';
require_once 'Zend/Http/Client/Adapter/Exception.php';
// Http_Clientロード
$client = new Zend_Http_Client();
try {
    $client->setUri($url);
    $client->setConfig(array(
     'maxredirects' => 5,
     'timeout'      => 30
    ));
    $params = array("id" => "hoge", "pass" => "hoge");
    // GET 複数のパラメータを一度に追加します
    $client->setParameterGet($params);
    // GET リクエストを実行します
    $response = $client->request();
} catch (Zend_Http_Client_Adapter_Exception $e) {
    // handle the error
    error_log($e);
}

サンプル(POST

require_once 'Zend/Http/Client.php';
require_once 'Zend/Http/Client/Adapter/Exception.php';
// Http_Clientロード
$client = new Zend_Http_Client();
try {
    $client->setUri($url);
    $client->setConfig(array(
     'maxredirects' => 5,
     'timeout'      => 30
    ));
    $params = array("id" => "hoge", "pass" => "hoge");
    // POST 複数のパラメータを一度に追加します
    $client->setParameterPost($params);
    // POST リクエストを実行します
    $response = $client->request(Zend_Http_Client::POST);
} catch (Zend_Http_Client_Adapter_Exception $e) {
    // handle the error
    error_log($e);
}

サンプル(https対応

$url = "https://login.yahoo.co.jp/config/login?.src=www&.done=http://www.yahoo.co.jp";
require_once 'Zend/Http/Client.php';
require_once 'Zend/Http/Client/Adapter/Socket.php';
require_once 'Zend/Http/Client/Adapter/Exception.php';
// Http_Clientロード
$client = new Zend_Http_Client();
try {
    $client->setUri($url);
    $client->setConfig(array(
     'maxredirects' => 5,
     'timeout'      => 30,
     'adapter' => 'Zend_Http_Client_Adapter_Socket',
     'ssltransport' => 'ssl',
     'strictredirects' => true,
    ));
    $params = array("id" => "hoge", "pass" => "hoge");
    // POST 複数のパラメータを一度に追加します
    $client->setParameterPost($params);
    // POST リクエストを実行します
    $response = $client->request(Zend_Http_Client::POST);
    $body = $response->getBody();
    $body = mb_convert_encoding($body, 'utf-8', 'euc-jp');
    echo $body;exit;
} catch (Zend_Http_Client_Adapter_Exception $e) {
    // handle the error
    error_log($e);
}