Home  >  Article  >  php教程  >  Zend Framework中如何判断URL是否设置了转发

Zend Framework中如何判断URL是否设置了转发

WBOY
WBOYOriginal
2016-05-25 16:43:411019browse

下面我来介绍一个Zend Framework中如何判断URL是否设置了转发,有需要学习的朋友可参考.

发送HTTP请求,代码如下:

<?php
$client = new Zend_Http_Client();
$client->setUri($url);
$client->setConfig(array(
    &#39;maxredirects&#39; => 1
));
$response = $client->request();
if ($response->isRedirect()) {
    echo "设置了转发";
} else {
    echo "没有设置转发";
}
?>

在这里如果不设置maxredirects参数,Zend默认的最大跳转数为5,就是在每次请求的时候,如果该地址设置了转发,他会读取转发到的新地址,然后再对这个地址发起请求,循环做这个操作,直至新地址没有设置转发或者循环超过了5次才会返回最后一次的请求数据.

这样的话,如果我只想获取某个域名是否设置了转发,那么就必须设置下maxredirects这个参数了.

Zend Framework代码如下:

<?php
public function request($method = null) {
    if (!$this->uri instanceof Zend_Uri_Http) {
        /** @see Zend_Http_Client_Exception */
        require_once &#39;Zend/Http/Client/Exception.php&#39;;
        throw new Zend_Http_Client_Exception(&#39;No valid URI has been passed to the client&#39;);
    }
    if ($method) {
        $this->setMethod($method);
    }
    $this->redirectCounter = 0;
    $response = null;
    // Make sure the adapter is loaded
    if ($this->adapter == null) {
        $this->setAdapter($this->config[&#39;adapter&#39;]);
    }
    // Send the first request. If redirected, continue.
    do {
        // Clone the URI and add the additional GET parameters to it
        //省略一万字
        
    } while ($this->redirectCounter < $this->config[&#39;maxredirects&#39;]);
    return $response;
}
?>


文章链接:

随便收藏,请保留本文地址!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn