下面我來介紹一個Zend Framework中如何判斷URL是否設置了轉發,有需要學習的朋友可參考.
發送HTTP請求,代碼如下:
- $client = new Zend_Http_Client();
- $client->setUri($url);
- $client->setConfig(array('maxredirects' => 1));
- $response = $client->request();
- if ( $response->isRedirect() ) {
- echo "設置了轉發";
- } else {
- echo "沒有設置轉發";
- }
在這里如果不設置maxredirects參數,Zend默認的最大跳轉數為5,就是在每次請求的時候,如果該地址設置了轉發,他會讀取轉發到的新地址,然后再對這個地址發起請求,循環做這個操作,直至新地址沒有設置轉發或者循環超過了5次才會返回最后一次的請求數據.
這樣的話,如果我只想獲取某個域名是否設置了轉發,那么就必須設置下maxredirects這個參數了.
Zend Framework代碼如下:
- public function request($method = null)
- {
- if (! $this->uri instanceof Zend_Uri_Http) {
- /** @see Zend_Http_Client_Exception */
- require_once 'Zend/Http/Client/Exception.php';
- throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
- }
- if ($method) {
- $this->setMethod($method);
- }
- $this->redirectCounter = 0;
- $response = null;
- // Make sure the adapter is loaded
- if ($this->adapter == null) {
- $this->setAdapter($this->config['adapter']);
- }
- // Send the first request. If redirected, continue.
- do {
- // Clone the URI and add the additional GET parameters to it
- //省略一萬字
- } while ($this->redirectCounter < $this->config['maxredirects']);
- return $response;
- }
新聞熱點
疑難解答