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

 更新时间:2016年11月25日 15:48  点击:1318
下面我来介绍一个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;
    }

我们有时刚安装好环境是会碰到PHP中php_ldap.dll不能加载,以前以为php_ldap.dll是默认加载的后来知道,解压的php是需要我们手动配置的。

将winntphp.ini-dist改名为php.ini,并找到;Windows Extensions项将

 代码如下 复制代码

;extension=php_ldap.dll

改成

extension=php_ldap.dll

如果有其它的可以

 代码如下 复制代码

extension_dir = c:php4
extension=php_zlib.dll
extension =php_ldap.dll
extension =php_zlib.dll
extension =php_calendar.dll
extension =php_exif.dll
extension =php_ftp.dll
extension =php_mssql70.dll (这一项不要加,没有MSSQL7.0呀)
extension =php_imap.dll

然后从php5中 把libeay32.dll和ssleay32.dll文件 复制到

 代码如下 复制代码

C:/Windows/System/System32
C:/Windows/System
C:/Windows

这样就可以使用了php_ldap了,查看是否安装上php_ldap.dll文件,打开phpinfo().就可以查看是否安装成功!

一般情况下您只需将其放入以下目录后“系统找不到php_ldap.dll”或者“没有php_ldap.dll的问题”就可以得到解决:


当然,如果您的操作系统没有安装到C盘,只需将路径中的C换作安装操作系统的盘符即可。如果是一般的应用程序找不到DLL文件,可能只需要将下载到的DLL文件拷贝到您启动的应用程序所在的目录。

json 数据格式数据用得最多的是与js,flash实时交互用的哦,那么php怎么返回json数据格式呢,下面我来分析一下实例。

php代码

 代码如下 复制代码

<?php
header('Content-type: text/json');

$fruits = array (
    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "holes"   => array("first", 5 => "second", "third")
);
echo json_encode($fruits);
?>

上面是英文是没有问题,如果是中文就会有问题,解决办法如下

Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。

 代码如下 复制代码

<?php
$array = array
 (
   'title'=>iconv('gb2312','utf-8','这里是中文标题'),
  'body'=>'abcd...'
 );

echo json_encode($array);
?>

结果

 代码如下 复制代码

{"title":"u8fd9u91ccu662fu4e2du6587u6807u9898","body":"abcd..."}

利用js来分析这个函数

 代码如下 复制代码

$(function(){
     $('#send').click(function() {
          $.getJSON('json.php', function(data) {
              $('#resText').empty();
   var html = '';
   $.each( data  , function(commentIndex, comment) {
    html += '<div class="comment"><h6>' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>';
   })
  $('#resText').html(html);
         })
    })
})

注意在你的php输出js格式时我们必须是header('Content-type: text/json');这样的头部信息发送哦。

后面加一个完整的可解析中文乱码的问题程序

 代码如下 复制代码

<?php
/**************************************************************
 *
 * 使用特定function对数组中所有元素做处理
 * @param string &$array  要处理的字符串
 * @param string $function 要执行的函数
 * @return boolean $apply_to_keys_also  是否也应用到key上
 * @access public
 *
 *************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }
 
        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}
 
/**************************************************************
 *
 * 将数组转换为JSON字符串(兼容中文)
 * @param array $array  要转换的数组
 * @return string  转换得到的json字符串
 * @access public
 *
 *************************************************************/
function JSON($array) {
 arrayRecursive($array, 'urlencode', true);
 $json = json_encode($array);
 return urldecode($json);
}

$array = array
       (
          'Name'=>'希亚',
          'Age'=>20
       );


echo JSON($array);
?>

本文章总结了php中数组的交集,并集,以及去除数组的重复项,以及从小到大排序函数,几乎都是对数组的操作有需要的朋友可参考参考。
 代码如下 复制代码
<?php
//两个数组的并集
$arr1 = array('a','b','c','d','e','f');
//$arr2 = array('a','a','e','a','p','a','a','e');
$arr2 = array('a','a','a','a');
$ilength = count($arr1);
$jlength = count($arr2);
 
/**
 * 两个数组的交集
 * @param array $arr1
 * @param array $arr2
 * @autho zhaoya
 * @return array $arr
 */
function jiaoji($arr1,$arr2)
{
 $ilength = count($arr1);
 $jlength = count($arr2);
 for($i=0;$i<$jlength;$i++)
 {
     for($j=0;$j<$ilength;$j++)
     {
         if($arr2[$i] == $arr1[$j])
         {
             $arr[] = $arr2[$i];
             break;
         }
     }
 }
 return $arr;
}
$arr3 = array(1,10,10,5,90,50,90);
$arr4 = array(10,23,50,100,110,80);
echo '<pre>';
$time1 = microtime();
 
/*$arr3 = __deleterepeat($arr3);
$arr4 = __deleterepeat($arr4);
$arr5 = bingji($arr3,$arr4);
$arrsort = sort_array($arr5);*/
 
///经过实验证明  先并集,然后在去除重复值 ,再排序,这样的速度会快一些 
///而先删除 两个数组的重复值,在并集,在排序的话,这样的速度慢点
$arr5 = bingji($arr3,$arr4);
$arr5 = __deleterepeat($arr5);
$arrsort = sort_array($arr5);
 
$time2 = microtime();
echo $time1,'<hr>';
echo $time2,'<hr>';
echo $time2-$time1;
 
print_r($arrsort);
//去除重复值(第一种方法)
//__deleterepeat($arr2);
 
 
/**
 * 去除重复值(第一种方法)
 * @param array $array
 * @return array $tmparr
 * @author zhaoya
 */
function __deleterepeat($array)
{
    $count = count($array);
    for($i = 0;$i<$count;$i++)
    {
        $change = false;
        for($j=$i+1;$j<$count;$j++)
        {
            if($array[$i] == $array[$j])
            {
                $change=true;
                break;
            }
        }
        if($change==false)
        {
            $tmparr[] = $array[$i];
        }
    }
    return $tmparr;
}
 
 
 
 
 
//去除重复的值 第二种方法
 
$arrayshift = _delrepeat($arr2);
$tmparray=array();
/***
 * 去除一维数组重复的值
 * @param array $arr
 * @return array $tmparray;
 * @author zhaoya
 */
function _delrepeat($arr)
{
    for($i=0;$i<count($arr);$i++)
    {
        if(inarray($arr[$i],$tmp))
        {
            $tmparray[] = $arr[$i];
        }
    }
    return $tmparray;
}
 
/**
 * 查找变量是否在这个数组里面
 * @param integer $num
 * @param array $arr
 * @author zhaoya
 * @return boolean
 *
 */
function inarray($num,$arr)
{
    if($arr)
    {
        for($i=0;$i<count($arr);$i++)
        {
            if($arr[$i] == $num)
            {
                return false;
            }
            return true;
        }
    }
    return true;
}
 
 
 
 
 
/**
 * 两个数组的并集
 * @param array $arr1  数组1
 * @param array $arr2  数组2
 * @author zhaoya
 * @return array $arr1
 */
function bingji($arr1,$arr2)
{
 $ilength = count($arr1);
 $jlength = count($arr2);
 for($i=0;$i<$jlength;$i++)
 {
     $change=false;
     for($j=0;$j<$ilength;$j++)
     {
         if($arr2[$i] == $arr1[$j])
         {
             $change = true;
             break;
         }
     }
     if($change == false)
     {
         $arr1[] = $arr2[$i];
     }
 }
 return $arr1;
}
 
/**
 * 数组排序 从小到大
 * @param array $arr  数组
 * @author zhaoya
 * @return array $arr
 */
function sort_array($arr)
{
 $length = count($arr);
 
 for($i=0;$i<$length;$i++)
 {
  for($j=$i+1;$j<$length;$j++)
  {
   if($arr[$i] > $arr[$j])
   {
    $tmp = $arr[$i];
    $arr[$i] = $arr[$j];
    $arr[$j] = $tmp;
   }
  }
 }
 return $arr;
}
 
 
 
 
?>
一个php初学者的一个学习笔记的面向对象编程实例,有需要学习的朋友可参考参考。
 代码如下 复制代码


class db {
    private $mysqli; //数据库连接
    private $options; //SQL选项
    private $tableName; //表名
    public function __construct($tabName) {
        $this->tableName = $tabName;
        $this->db ();
    }
    private function db() {
        $this->mysqli = new mysqli ( 'localhost', 'root', '', 'hdcms' );
        $this->mysqli->query("SET NAMES GBK");
    }
    public function fields($fildsArr) {
        if (empty ( $fildsArr )) {
            $this->options ['fields'] = '';
        }
        if (is_array ( $fildsArr )) {
            $this->options ['fields'] = implode ( ',', $fildsArr );
        } else {
            $this->options ['fields'] = $fildsArr;
        }
        return $this;
    }
    public function order($str) {
        $this->options ['order'] = "ORDER BY " . $str;
        return $this;
    }
    public function select() {
        $sql = "SELECT {$this->options['fields']} FROM {$this->tableName}  {$this->options['order']}";
        return $this->query ( $sql );
    }
    private function query($sql) {
        $result = $this->mysqli
            ->query ( $sql );
        $rows = array ();
        while ( $row = $result->fetch_assoc () ) {
            $rows [] = $row;
        }
        return $rows;
    }
    private function close() {
        $this->mysqli
            ->close ();
    }
    function __destruct() {
        $this->close ();
    }
}
$chanel = new db ( "hdw_channel" );
$chanelInfo = $chanel->fields ( 'id,cname,cpath' )
    ->select ();
echo "<pre>";
print_r ( $chanelInfo );

class a {
    protected  function aa(){
        echo 222;
    }
}
class b extends a{
    function bb(){
        $this->aa();
    }
}
$c = new b();
$c->bb();

public   公有的:本类,子类,外部对象都可以调用
protected 受保护的:本类 子类,可以执行,外部对象不可以调用
private 私有的:只能本类执行,子类与外部对象都不可调用

[!--infotagslink--]

相关文章