curl in php captures web page response data

小云云
Release: 2023-03-21 20:54:02
Original
2220 people have browsed it

This article mainly shares with you the method of using curl to capture web page response data in PHP. I hope it can help you.

<?php
namespace app\index\model;
use think\Model;

class httpPost extends Model{
 /**
 * POST 请求 抓取数据

 * @param string $url


 * @param array $param


 * @return string content


 */
	public function httpPost($url, $param, $cookie_file) {


	   $oCurl = curl_init ();


	   if (stripos ( $url, "https://" ) !== FALSE) {


	      curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );


	      curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );


	   }


	   if (is_string ( $param )) {


	      $strPOST = $param;


	   } else {


	      $aPOST = array ();


	      foreach ( $param as $key => $val ) {


	         $aPOST [] = $key . "=" . urlencode ( $val );


	      }


	      $strPOST = join ( "&", $aPOST );


	   }


	   curl_setopt ( $oCurl, CURLOPT_URL, $url );


	   curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );


	   curl_setopt ( $oCurl, CURLOPT_POST, true );


	   curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
	   
	   
	 //   curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);// 过滤htpts
	 //   curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);// 过滤htpts
	 //   $UserAgent = &#39;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)&#39;;
		// curl_setopt($oCurl, CURLOPT_USERAGENT, $UserAgent); // 使用代理
		// //
		// curl_setopt($oCurl, CURLOPT_HEADER, false);


		// curl_setopt($oCurl, CURLOPT_HEADER, 0);
     	
  		curl_setopt($oCurl, CURLOPT_COOKIEFILE, $cookie_file);


	   $sContent = curl_exec ( $oCurl );


	   $aStatus = curl_getinfo ( $oCurl );


	   curl_close ( $oCurl );


	   if (intval ( $aStatus ["http_code"] ) == 200) {


	      return $sContent;


	   } else {

	      return false;

	   }

	}
}
Copy after login

Related recommendations:

How to obtain CURL request headers and response headers in php

Detailed explanation of CURL for PHP web crawlers

CURL in php gets request headers and response headers

The above is the detailed content of curl in php captures web page response data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!