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 = '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)'; // 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; } } }
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!