| 
       
	WordPress的采集插件挺多, 功能也很强大。但是如果要折腾,还是自己写 :D 
	下面是我的采集插件里的几个关键函数: 
	匹配函数(经测试效率最好的一个) 
	/** 
	 * 获取第一个匹配的内容 
	 * 
	 * @author        Copyright (c) <http://sjolzy.cn> 
	 * @param        string    $str    内容 
	 * @param        string    $start    起始匹配 
	 * @param        string    $end    中止匹配 
	 * @return        string 
	 */ 
	private function strCut($str, $start, $end){ 
	    $content = strstr( $str, $start ); 
	    $content = substr( $content, strlen( $start ), strpos( $content, $end ) - strlen( $start ) ); 
	    return $content; 
	} 
	取出所有匹配的内容 
	/** 
	 * 获取所有匹配的内容 
	 * 
	 * @author        Copyright (c) <http://sjolzy.cn> 
	 * @param        string    $str    内容 
	 * @param        string    $start    起始匹配 
	 * @param        string    $end    中止匹配 
	 * @return        array 
	 */ 
	private function strCutAll($str,$start,$end){ 
	    $content    = explode($start,$str); 
	    $matchs        = array(); 
	    $sum        = count($content); 
	    for( $i = 1;$i < $sum;$i++ ){ 
	        $tmp = explode($end,$content[$i]); 
	        $matchs[] = $tmp[0]; 
	        unset($tmp); 
	    } 
	    return $matchs; 
	} 
	通过上面两个函数, 在使用preg_match_all配合正则表达式, 想要的网页内容基本都能采集到. 
	效率相对比较高的一个采集网页的函数 
	/** 
	 * 根据URL采集网页内容 
	 * 
	 * @author        Copyright (c) <http://sjolzy.cn> 
	 * @param        string    $url    链接地址 
	 * @return        string 
	 */ 
	private function fetch($url){ 
	    $handle = fopen($url, 'r'); 
	    $content = ''; 
	    while (!feof($handle)){ 
	        $content .= fgets($handle, 10000); 
	    } 
	    return $content?$this->utf8_iconv($content):''; 
	} 
	//这里的utf8_iconv函数是把GB2312转成UTF-8编码 
	上面是我的采集插件里的主要函数, 内容采集到手后, 就是各种入库. 
	WordPress程序采集分类并自动入库 
	//添加目录 
	private function addCat($name,$slug,$parent=0,$desc=''){ 
	    $cat = array( 
	            'cat_name'                => $name, 
	            'category_description'    => $desc, 
	            'category_nicename'        => $slug, 
	            'category_parent'        => $parent 
	        ); 
	    return wp_insert_category ($cat); 
	} 
	WordPress程序采集并保存文章 
	  
	//添加日志 
	private function addPost($title,$content,$slug='',$cats=array()){ 
	    $post = array( 
	        'post_title'        => $title, 
	        'post_name'            => $slug, 
	        'post_content'        => $content, 
	        'post_status'        => 'publish', 
	        'post_author'        => 1, 
	        'post_category'        => $cats 
	    ); 
	    return wp_insert_post ( $post ); 
	} 
	自动设置文章的特色图片 
	$img    = '网络图片地址'; 
	$p1    = '文章ID';//addPost返回 
	$attachment_id    = $this->addAttachment($img,$p1); 
	add_post_meta($p1, '_thumbnail_id', $attachment_id, true); 
	综上, 可见WordPress程序采集文章并不难.. 
      
      (责任编辑:最模板) |