찾다
php教程php手册PHP之抓取网页数据

PHP之抓取网页数据

Jun 06, 2016 pm 08:01 PM
php회사고객개발하다기다데이터웹페이지맞잡다프로젝트

最近在公司开发一项目、配合客户端人员开发接口,接口中需要接受客户端人员传递过来的头部信息,所以在浏览器中访问这个接口就无法访问、因为你传递的参数不全,最近想了一个方法:那就是在本地模拟一些数据提交,这个时候本人用到的是fsockopen函数,这个函

        最近在公司开发一项目、配合客户端人员开发接口,接口中需要接受客户端人员传递过来的头部信息,所以在浏览器中访问这个接口就无法访问、因为你传递的参数不全,最近想了一个方法:那就是在本地模拟一些数据提交,这个时候本人用到的是fsockopen函数,这个函数挺不错的。。




       function index(){
		
		$posturl = "XXX";
		$Did = '352110051958276';
		$Key = md5($Did.$this->Keym);
		$ms = $this->require_by_socket("Mm","action",$posturl,$Did,$Key);
		//$this->display();
		echo  $ms;
	}
	function require_by_socket($action,$method,$posturl,$Did,$Key) {
		
		$fp = fsockopen ( "$posturl", 80, $errno, $errstr, 30 );
		if (! $fp) {
			echo "$errstr ($errno)<br>\n";
		} else {
			
			//$params = array ('did' => 1009, 'ver' => '199/9/201208','filmid' => 18 );
			//$post = http_build_query($params);
			$post = "did=352110051958276&ver=199/22/20121222&filmid=18&model=HTC&os=and&pid=1&refresh=1";
			//echo $post;exit;
			$header = "POST /service/index.php/$action/$method HTTP/1.1\r\n";
			$header .= "Host: $posturl\r\n";
			$header .= "Content-Type:application/x-www-form-urlencoded" . "\r\n";
			$header .= "Content-Length: " . strlen ( $post ) . "\r\n";
			$header .="Did:$Did\r\n";
			$header .="Key:$Key\r\n";                      //这里就是 提交header头部信息
			$header .= "Connection: Close\r\n\r\n";
			$header .= $post."\r\n";	
			$inheader = 1;
			fputs ( $fp, $header );
			while ( ! feof ( $fp ) ) {
				
				$result = fgets ( $fp, 1024 ); //去除请求包的头只显示页面的返回数据
				if ($inheader && ($result == "\n" || $result == "\r\n")) {
					$inheader = 0;
				}
				//echo $inheader;exit;
				if ($inheader == 0) {
					$info .= $result;
				}
			}
			fclose ( $fp );
			return $info;
			
			
		}
	}

备注:以上
$header .="Did:$Did\r\n";
$header .="Key:$Key\r\n"; 

这些参数其实就是提交开发者想要的头部参数信息,然后您可以通过函数处理这样参数 就可以获取您想要的。





还分享下另外的一种方式:


CURL:

 

<strong><span><?php function curlGetData($url,$header)
{
	$ch = curl_init() or die (curl_error());
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);    //设置http头   
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$data=curl_exec($ch) or die (curl_error());
	curl_close($ch);
	return $data;	
}
$header[]='Pid:1';
$header[]='Ver:100/26/200000';
$header[]='Key:'.xxxxx;
$header[]='Did:test';

$url="http://xxxx.com/Yxx/xxxx?id=65761723&type=12&";
echo curlGetData($url,$header);</span></span></strong>







성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.