Zend_http_Client学习

//取得一个Zend_Http_Client对象,并进行配置
$client = new Zend_Http_Client(
'http://example.org', //目标主机
array(
'maxredirects' => 0, 
'timeout' => 30 //链接超时时间
)
);
//发送HTTP请求,并将返回的数据保存到$response
$response = $client->request(); //默认为GET方法,可以在request()函数中指定发送请求的方法。
//$response = $client->request("POST");

Zend_Http_Client 配置参数

参数描述值的类型缺省值
maxredirects 随后的重定向的最大值 (0 = none) 整数 5
strict 是否执行头部名称的确认,当设置为 False 时,将忽略确认,通常情况下不应改变这个参数的值。 布尔值 true
strictredirects 重定向时是否严格遵守 RFC (见 第 19.2.1 节 “HTTP Redirections”) 布尔值 false
useragent 用户代理的识别字符串(含在请求的头部信息内) 字符串 'Zend_Http_Client'
timeout 连接超时 (单位是秒) 整数 10
httpversion HTTP 协议版本 (通常是 '1.1' 或 '1.0') 字符串 '1.1'
adapter 连接适配器类时使用(见 第 19.3 节 “Zend_Http_Client - Connection Adapters” 多种类型 'Zend_Http_Client_Adapter_Socket'
keepalive 是否允许与服务器之间的 keep-alive 连接。如果在同一台服务器上 执行几个互相关联的请求时,keep-alive 连接是有用的而且有可能提高性能。 布尔值 false
storeresponse 是否保存上次的响应结果,以备今后使用getLastResponse()重新获取。如果设置为 false,getLastResponse() 将返回空。 布尔值 true
//为GET方法设置参数
// 使用 setParameterGet 方法设置一个 GET 参数
$client->setParameterGet('knight', 'lancelot');
// 设置 URL 的等效方法
$client->setUri('http://example.com/index.php?knight=lancelot');

//为POST方法设置参数
// 设置一个 POST 参数
$client->setParameterPost('language', 'fr');

// 设置几个 POST 参数,其中的一个参数有几个值
$client->setParameterPost(array(
'language' => 'es',
'country' => 'ar',
'selection' => array(45, 32, 80)
));

//使用COOKIE

$client->setCookie('flavor', 'chocolate chips'); //Cookie名称与值
$client->setCookie('flavor=chocolate%20chips'); //Cookie值必须经过URL编码

// By providing a Zend_Http_Cookie object
$cookie = Zend_Http_Cookie::fromString('flavor=chocolate%20chips');
$client->setCookie($cookie);

//定义头信息

// Setting a single header, overwriting any previous value
$client->setHeaders('Host', 'www.example.com');

// Another way of doing the exact same thing
$client->setHeaders('Host: www.example.com');

// Setting several values for the same header
// (useful mostly for Cookie headers):
$client->setHeaders('Cookie', array(
'PHPSESSID=1234567890abcdef1234567890abcdef',
'language=he'
));

//上传文件

// Uploading arbitrary data as a file
$text = 'this is some plain text';
$client->setFileUpload('some_text.txt', 'upload', $text, 'text/plain');

// Uploading an existing file
$client->setFileUpload('/tmp/Backup.tar.gz', 'bufile');

// Send the files
$client->request('POST');

//发送原始POST数据

$xml = '<book>' .
' <title>Islands in the Stream</title>' .
' <author>Ernest Hemingway</author>' .
' <year>1970</year>' .
'</book>';

$client->setRawData($xml, 'text/xml')->request('POST');

// Another way to do the same thing:
$client->setRawData($xml)->setEncType('text/xml')->request('POST');


//HTTP认证

// Using basic authentication
$client->setAuth('shahar', 'myPassword!', Zend_Http_Client::AUTH_BASIC);

// Since basic auth is default, you can just do this:
$client->setAuth('shahar', 'myPassword!');



//使用HTTPS传输层


// Set the configuration parameters
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Socket',
'ssltransport' => 'tls'
);

// Instantiate a client object
$client = new Zend_Http_Client('https://www.example.com', $config);

// The following request will be sent over a TLS secure connection.
$response = $client->request();


使用代理发送HTTP请
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Proxy', //设置适配器
'proxy_host' => 'proxy.int.zend.com',       //设置代理网址
'proxy_port' => 8000,               //设置代理服务器商品
'proxy_user' => 'shahar.e', //设置代理服务器用户名
'proxy_pass' => 'bananashaped' //设置代理服务器密码
);

//使用代理服务器,向网址发送请求。
$client = new Zend_Http_Client('http://www.example.com', $config);
原文地址:https://www.cnblogs.com/zfying/p/2612240.html