Home  >  Article  >  Backend Development  >  php capture pictures

php capture pictures

不言
不言Original
2018-04-14 11:58:261908browse

This article mainly introduces php to capture images, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

function actionGetimg(){
		ob_end_clean();
		ob_implicit_flush(1);
		//13050
		for($a=1;$a<13074;$a++){
			echo $a.&#39;+&#39;;
			echo &#39;<br/>&#39;;
			$url = "http://www.jtyxzz.com/portal.php?mod=view&aid=".$a;
			$arr = $this->file_from_url_content($url);
			$regex4 = "/<img src=\"data\/[\s\S]*?\">/";//标题
			preg_match_all($regex4, $arr, $result1);
			//var_dump($result1);
			//var_dump(empty($result1[0]));exit;
			if (!empty($result1[0])) {
				foreach ($result1 as $key => $val) {
					$first = str_replace(&#39;<img src="&#39;, &#39;&#39;, $val);
					$second = str_replace(&#39;">&#39;, &#39;&#39;, $first);
				}
				//dump($second);exit;
				foreach ($second as $key => $val) {
					//var_dump($val);
					//echo &#39;<br/>&#39;;
					$zuihou=strrpos($val,&#39;/&#39;);
					$mulu=substr($val,0,$zuihou);
					//$ming=substr($val,$zuihou+1);
					//dump($ming);exit;
					if(is_dir($mulu)){
						//echo &#39;cunzai&#39;;
					}else{//目录不存在创建目录
						//echo &#39;bucunzai&#39;;exit;
						mkdir($mulu,0777,true);
					}


					$image =@file_get_contents(&#39;http://www.jtyxzz.com/&#39; . $val);
					if($image){
						file_put_contents($val, $image);
						//echo $val;exit;
					}else{
						file_put_contents(&#39;images/diushi.txt&#39;,$a.&#39;/&#39;.$val.&#39;图片丢失<br/>&#39;,FILE_APPEND);
                        echo $a.&#39;/&#39;.$val.&#39;图片丢失&#39;;
						echo &#39;<br/>&#39;;
					}


				}
			}else {
                echo $a.&#39;no img or no permission&#39;;
				echo &#39;<br/>&#39;;
			}
			$sleep=$a/100;
			if(is_int($sleep)){
				echo $sleep;
				echo &#39;<br/>&#39;;
				echo $a;
				sleep(rand(40,50));
			}
		}
		echo &#39;图片采集完毕&#39;;
		//var_dump($second);exit;
       exit;
	}

/**
	 * 异步将远程链接上的内容
	 * @param unknown $url 远程地址
	 * @param unknown $saveName 保存在服务器上的文件名
	 * @param unknown $path 保存路径
	 * @return boolean
	 */
	function file_from_url_content($url) {
		// 设置运行时间为无限制
		set_time_limit ( 0 );
		$url = trim ( $url );
		$curl = curl_init ();
		// 设置你需要抓取的URL
		curl_setopt ( $curl, CURLOPT_URL, $url );
		// 设置header
		curl_setopt ( $curl, CURLOPT_HEADER, 0 );
		// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		// 运行cURL,请求网页
		$file = curl_exec ( $curl );
		// 关闭URL请求
		curl_close ( $curl );
		return $file;
	}

Related recommendations:

Example of method of using PHP to capture Baidu Reading

php capture page garbled analysis

The above is the detailed content of php capture pictures. For more information, please follow other related articles on the PHP Chinese website!

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