Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1261736
  • 博文数量: 315
  • 博客积分: 10397
  • 博客等级: 上将
  • 技术积分: 3731
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 21:21
文章分类

全部博文(315)

文章存档

2015年(10)

2014年(3)

2013年(2)

2012年(8)

2011年(8)

2010年(29)

2009年(59)

2008年(77)

2007年(119)

分类:

2008-02-27 15:28:48

一、HTTP Redirections

缺省情况下,Zend_Http_Client自动操作HTTP重定向,直至5次重定向。该值可被配置参数‘maxredirects’来设定。

根据HTTP1.1协议,HTTP301302响应应该被客户端使用同样的请求方式重新发送同样的请求到指定的地点。然而,大部分的客户端并没有照此执行并且当重新指向的时候总是使用GET请求。缺省情况下,Zend_Http_Client做同样的事情--当遇到一个301302响应的重指向的时候,所有GETPOST参数被重设定并且一个GET请求被发送到一个新地点。该行为能被配置参数‘strictredirects’设定为布尔TRUE改变。

 

你在发送一个请求后可以获得重指向的次数,通过getRedirectionsCount()方法

 

二、添加一个Cookie和使用Cookie的持续性

Zend_Http_Client提供了一个添加cookie进你的请求的简易接口,那样的话,没有直接报头的更正是必须的。可以使用setCookie()方法


// Easy and simple: by providing a cookie name and cookie value
$client->setCookie('flavor''chocolate chips');

// By directly providing a raw cookie string (name=value)
// Note that the value must be already URL encoded
$client->setCookie('flavor=chocolate%20chips');


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

Zend_Http_Client也提供了cookie stickiness的方法――客户端内部存储所有发送和接收的cookie,并且在并发请求上自动重新发送它们。这点很有用,比如当你需要第一次登陆一个远程站点,在发送更进一步的请求的时候,接受和验证sessionID cookie

 

 


// To turn cookie stickiness on, set a Cookie Jar
$client->setCookieJar();

// First request: log in and start a sessio
$client->setUri('');
$client->setParameterPost('user''h4x0r');
$client->setParameterPost('password''1337');
$client->request('POST');


// The Cookie Jar automatically stores the cookies set
// in the response, like a session ID cookie.



// Now we can send our next request - the stored cookies
// will be automatically sent.
$client->setUri('');
$client->request('GET');
 

设定自定义的请求报头

使用setHeader方法


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

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

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

setHeader()也可以在一次调用时设定多个报头,通过提供一个报头的数组作为一个单独参数

 

// Setting multiple headers, overwriting any previous value
$client->setHeaders(array(
    
'Host' => '',
    
'Accept-encoding' => 'gzip,deflate',
    
'X-Powered-By' => 'Zend Framework'));


// The array can also contain full array strings:
$client->setHeaders(array(
    
'Host: ',
    
'Accept-encoding: gzip,deflate',
    
'X-Powered-By: Zend Framework'));

文件上传

你可以透过HTTP使用setFileUpload方法来上传文件。该方法将一个文件名作为第一个参数,一个form名字作为第二个参数,数据作为第三个可选参数。如果第三个数据参数为null,第一个文件名参数将被考虑是磁盘是一个真实文件,Zend_Http_Client将试图读取并上传该文件。如果数据参数不为空,第一个文件名参数将作为文件名被传送,但不是磁盘上的实际文件。第二个form名是必须的,并且等同与input标签的name属性。如果文件是通过htmlform被传送,第四个可选参数将提供该文件的内容类型。如果没有指定,Zend_Http_Client会从磁盘中读取该文件。Mime_content_type方法将被用来猜测文件的内容类型,如果得不到,缺省的MIME类型将会是application/octet-stream.


// 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->submit('POST');
 

第一个例子,$text变量被上传并且将会在服务器端以$_FILES[‘upload’]得到。第二个例子中,已存在的文件/tmp/Backup.tar.gz被上传到服务器端以$_FILES[‘bufile’]得到。内容类型如果可以将自动猜测,如果不能,内容类型被设定为application/octet-stream.

 

当上传文件时,HTTP请求内容类型自动设定为multipart/form-data.记得你必须发送一个POST或是PUT请求来上传文件。如果以其他请求方式的话,大部分服务器将忽略请求实体

 

发送原始POST数据

你可以使用Zend_Http_Client发送原始POST数据通过setRawData方法。该方法有两个参数:第一个是数据发送到请求实体中。第二个可选参数是数据的内容类型。不过参数是可选的,通常应该在发送请求之前设定:要不使用setRawData,要不使用另外一个方法setEncType


$xml 
'.
       
'  Islands in the Stream.
       
'  Ernest Hemingway.
       
'  1970.
       
'';

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

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

数据在服务器端可以通过PHP$HTTP_RAW_POST_DATA变量或是php://input流获得

 

设定原始POST数据为发送请求将忽略任何POST参数或文件上传。你不能同时使用两种作为请求访问。要当心大部分服务器将忽略请求实体除非你发送一个POST请求


HTTP 认证

目前,Zend_Http_Client仅仅支持基本的HTTP验证。该特性使用setAuth方法。该方法有三个函数:用户名,密码和可选的验证类型参数。作为提醒,目前仅支持基本的验证(digest验证支持已计划开发)



// 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!');

同一个客户端发送多个请求

Zend_Http_Client也被特别的设计用同一个对象操作几个连续的请求。这点很有用,比如当一个脚本所需的数据需要从几个地方获取,或者当访问一个指定的HTTP资源需要登陆获取一个session cookie

当对同一个主机操作不同的请求时,非常推荐开启’keepalive’配置标签。这样的话,如果服务器支持keep-alive连接,只有当所有请求完成和Client对象被销毁时,服务器才会关闭连接。这样就避免重复的开启和关闭到服务器的连接。

resetParameters方法被使用时,注意非请求特定的头是不会重设的。事实上,只有’Content-length’’Content-type’被重设。这让你设定一次就可以不管了,比如’Accept-language’ ’Accept-encoding’报头。

另一项针对持续请求特别设计的特性是Cookie Jar对象。Cookie Jar允许你自动保存cookie是在第一次请求时由服务器设定的,并且以透明的持续请求方式发送他们。这使得我们可以在发送一个实际的数据获取请求之前通过一个认证。

如果你的应用程序需要对每一个用户进行验证请求,并且在你的应用程序中,持续的请求也许不只由一个脚本发起,这时保存Cookie Jar对象至用户的session中也许会是一个好主意。这样,你只要对每个用户的session进行验证就好了。


// First, instantiate the client
$client = new Zend_Http_Client('http:///fetchdata.php', array(
    
'keepalive' => true
));
// Do we have the cookies stored in our session?
if (isset($_SESSION['cookiejar']) &&
    
$_SESSION['cookiejar'] instanceof Zend_Http_CookieJar)) {
    
$client->setCookieJar($_SESSION['cookiejar']);
} else {
    
// If we don't, authenticate and store cookies
    
$client->setCookieJar();
    
$client->setUri('http:///login.php');
    
$client->setParameterPost(array(
        
'user' => 'shahar',
        
'pass' => 'somesecret'
    
));
    
$client->request(Zend_Http_Client::POST);
    
// Now, clear parameters and set the URI to the original one
    // (note that the cookies that were set by the server are now
    // stored in the jar)
    
$client->resetParameters();
    
$client->setUri('http:///fetchdata.php');
}

$response $client->request(Zend_Http_Client::GET);


// Store cookies in session, for next page
$_SESSION['cookiejar'] = $client->getCookieJar();

 

 

阅读(3341) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~