100积分,求一个curl代码,谢谢大家 https://yobeyi.tmall.com/i/asynSearch.htm?_ksTS=1491742553039_117&callback=jsonp118&mid=w-14600885200-0&wid=14600885200求获得上面网页中的商品图片、商品标题、价格、总销量,curl后变成下面这个样子: 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 function request($url,$https=true,$method='get',$data=null){ //1.初始化curl $ch = curl_init($url); //2.设置相关的参数 //字符串不直接输出,进行一个变量的存储 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_setopt($ch, CURLOPT_HEADER, 1); //判断是否为https请求 if($https === true){ //为了确保https请求能够请求成功 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } //判断是否为post请求 if($method == 'post'){ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } //3.发送请求 $str = curl_exec($ch); // $hd = curl_getinfo($ch); //4.关闭连接 curl_close($ch); //返回请求到的结果 // return array('str'=>$str,'hd'=>$hd); return $str; }直接调用request()就可以了。我一直用这个 $url = 'https://yobeyi.tmall.com/i/asynSearch.htm?_ksTS=1491742553039_117&callback=jsonp118&mid=w-14600885200-0&wid=14600885200'; request($url); 这都成一个页面了, 何止是一个curl? 抓取这个不需要 curl,文件函数就可以header('Content-type: text/html;charset=GBK');$url = 'https://yobeyi.tmall.com/i/asynSearch.htm?_ksTS=1491742553039_117&callback=jsonp118&mid=w-14600885200-0&wid=14600885200';$s = file_get_contents($url);$s = substr($s, 16, -2);echo stripslashes($s);取到的是 html 代码,他原本是 jsonp 回调接下来如何分析,就与抓取无关了 其实cURL是采集类,直接抓取是全部网页的所有html内容,将获取的内容进行正则匹配,就可以获得你想要的数据,再按照你自定义的排版要求对你想要的数据进行遍历输出就OK了。 function doCurl($url, $data=array(), $header=array(), $timeout=30){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); return $response;}$url = 'https://yobeyi.tmall.com/i/asynSearch.htm?_ksTS=1491742553039_117&mid=w-14600885200-0&wid=14600885200';$data = doCurl($url);echo stripslashes(iconv('GBK', 'UTF8', $data)); <?phpclass Http{ public $httpVersion = "1.1"; //http协议版本(1.0/1.1) public $cookieFile = ""; //存放cookie的文件路径 public $headers = array(); //头部信息(发送的) public $header; //头部信息(接收的) public $timeout = 10; //超时时间 public $encode = true; //post数据是否重新编码 public $cookie = ""; //返回的cookie public $proxy = ""; public $info = ""; //---构造函数 function __construct() { $this->headers["Accept"] = "Accept: text/html, application/xhtml+xml, */*"; $this->headers["Accept-Encoding"] = "Accept-Encoding: gzip, deflate"; $this->headers["Accept-Language"] = "Accept-Language: zh-cn"; $this->headers["Connection"] = "Connection: Keep-Alive"; $this->headers["Referer"] = ""; $this->headers["User-Agent"] = "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)"; } //---GET请求 function get($url,$referer="",$cookie="",$jump=1,$returnCode="") { return $this->curl('GET',$url,$referer,$cookie,$jump,$returnCode,""); } //---POST请求 function post($url,$referer="",$cookie="",$jump=1,$returnCode="",$data= "") { return $this->curl('POST',$url,$referer,$cookie,$jump,$returnCode,$data); } //---CURL请求 function curl($method,$url,$referer,$cookie,$jump,$returnCode,$data="") { if($referer) $this->headers["Referer"] = "Referer: {$referer}"; $ch = curl_init(); if(stripos($url,"https://") == 0) { curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSLVERSION, 1); } if($data && $this->encode) { $data = $this->encodePostData($data); } if($method == 'POST') { curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); } curl_setopt($ch,CURLOPT_URL,$url); //请求地址url curl_setopt($ch,CURLOPT_HTTP_VERSION,($this->httpVersion == "1.0" ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1) );//设置http版本 curl_setopt($ch, CURLOPT_PROXY,$this->proxy); //设置代理 curl_setopt($ch,CURLOPT_TIMEOUT,$this->timeout); // 置超时限制防止死循环 curl_setopt($ch,CURLOPT_AUTOREFERER,true); //自动设置Referer curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$jump); //301或302...自动跳转 curl_setopt($ch,CURLOPT_HEADER,1); //启用时会将头文件的信息作为数据流输出 curl_setopt($ch, CURLOPT_NOBODY,false); //启用时将不对HTML中的BODY部分进行输出 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//设定返回的数据是否自动显示 curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookieFile); curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookieFile); $this->headers = array_filter($this->headers,create_function( '$v ', 'return !empty($v); ')); curl_setopt($ch,CURLOPT_HTTPHEADER,$this->headers); $str = @curl_exec($ch); $this->info = curl_getinfo($ch); curl_close($ch); unset($ch); $this->header = substr($str,0,$this->info['header_size']); $body = substr($str,$this->info['header_size']); $this->cookie = ""; if(preg_match_all('/Set-Cookie:([\s\S]*?)(;|[\r\n])/m',$this->header,$match)) { foreach($match[1] as $val) { $this->cookie .= trim($val) . ';'; } } if(stripos($this->header,'Content-Encoding: gzip')) { if (function_exists('gzdecode')) { $body = gzdecode($body); } else { $body = gzinflate(substr($data,10,-8)); } } return $this->safeEncoding($this->header,$body,$returnCode); } //--POST数据格式化编码 function encodePostData($data) { if(is_array($data)) return http_build_query($data); $encoded = ''; foreach(explode("&",$data) as $val) { $temp = explode("=",$val); $encoded .= $encoded ? "&" : ""; $encoded .= rawurlencode($temp[0]) . "=" . rawurlencode(@$temp[1]); } return $encoded; } //---返回安全编码utf-8 function safeEncoding($header,$body_source,$code="") { if($code) { $code = $code; } else if(preg_match("/charset=(\S+)/i",$header,$match)) { $code = $match[1]; } else if(preg_match("/<meta(?:.[^>]*)charset\s*=([a-zA-Z0-9-_]+)/i",$body_source,$match)) { $code = $match[1]; } else { $code = strtolower($this->checkEncoding($body_source)); } $code = strtolower($code); if($code == "gb2312") { $code = "gbk"; } $body = ($code == "utf-8" ? $body_source : mb_convert_encoding($body_source,'utf-8',$code)); return $body; } //--检查判断编码 function checkEncoding($str) { if($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) { return "utf-8"; } else { return "gbk"; } } }?> function curl_post($url,$data,$header=null){ $ch = curl_init(); $header[] = "Accept-Charset: utf-8"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(strpos($url,'https') != false){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https $opt[CURLOPT_SSL_VERIFYHOST] = 1; $opt[CURLOPT_SSL_VERIFYPEER] = FALSE; } if($header){ curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);// curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); if(is_array($data)) $data = http_build_query($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); //关闭curl curl_close($ch); return $result;}一个比较简单的curl POST请求的方法 关于PHP获取MP3文件总长度时间(在线等待高手指导) 关于ajax的问题,高手请进! php页面空白 smarty问题 如何抓去aspx网页的图片啊? 安装wamp5后就是最新版的集成(apache,mysql,php),登录phpmyadmin,出现错误 帮帮我,一个关于日期显示的问题。 %%%%%%%%%%%十万火急,请高手帮忙! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 大家认为面试的时候最容易出的PHP考题是什么方向? php中tcp的连接 PHP插入没反应,在AMFPHP1.9 这段中重复的implode函数该怎么解决呢
//1.初始化curl
$ch = curl_init($url);
//2.设置相关的参数
//字符串不直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HEADER, 1);
//判断是否为https请求
if($https === true){
//为了确保https请求能够请求成功
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
//判断是否为post请求
if($method == 'post'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//3.发送请求
$str = curl_exec($ch);
// $hd = curl_getinfo($ch);
//4.关闭连接
curl_close($ch);
//返回请求到的结果
// return array('str'=>$str,'hd'=>$hd);
return $str;
}直接调用request()就可以了。我一直用这个
request($url);
$url = 'https://yobeyi.tmall.com/i/asynSearch.htm?_ksTS=1491742553039_117&callback=jsonp118&mid=w-14600885200-0&wid=14600885200';
$s = file_get_contents($url);
$s = substr($s, 16, -2);
echo stripslashes($s);取到的是 html 代码,他原本是 jsonp 回调
接下来如何分析,就与抓取无关了
function doCurl($url, $data=array(), $header=array(), $timeout=30){ $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $response = curl_exec($ch); if($error=curl_error($ch)){
die($error);
} curl_close($ch); return $response;}$url = 'https://yobeyi.tmall.com/i/asynSearch.htm?_ksTS=1491742553039_117&mid=w-14600885200-0&wid=14600885200';
$data = doCurl($url);echo stripslashes(iconv('GBK', 'UTF8', $data));
class Http
{
public $httpVersion = "1.1"; //http协议版本(1.0/1.1)
public $cookieFile = ""; //存放cookie的文件路径
public $headers = array(); //头部信息(发送的)
public $header; //头部信息(接收的)
public $timeout = 10; //超时时间
public $encode = true; //post数据是否重新编码
public $cookie = ""; //返回的cookie
public $proxy = "";
public $info = "";
//---构造函数
function __construct()
{
$this->headers["Accept"] = "Accept: text/html, application/xhtml+xml, */*";
$this->headers["Accept-Encoding"] = "Accept-Encoding: gzip, deflate";
$this->headers["Accept-Language"] = "Accept-Language: zh-cn";
$this->headers["Connection"] = "Connection: Keep-Alive";
$this->headers["Referer"] = "";
$this->headers["User-Agent"] = "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
}
//---GET请求
function get($url,$referer="",$cookie="",$jump=1,$returnCode="")
{
return $this->curl('GET',$url,$referer,$cookie,$jump,$returnCode,"");
}
//---POST请求
function post($url,$referer="",$cookie="",$jump=1,$returnCode="",$data= "")
{
return $this->curl('POST',$url,$referer,$cookie,$jump,$returnCode,$data);
}
//---CURL请求
function curl($method,$url,$referer,$cookie,$jump,$returnCode,$data="")
{
if($referer) $this->headers["Referer"] = "Referer: {$referer}";
$ch = curl_init();
if(stripos($url,"https://") == 0)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
}
if($data && $this->encode)
{
$data = $this->encodePostData($data);
}
if($method == 'POST')
{
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($ch,CURLOPT_URL,$url); //请求地址url
curl_setopt($ch,CURLOPT_HTTP_VERSION,($this->httpVersion == "1.0" ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1) );//设置http版本
curl_setopt($ch, CURLOPT_PROXY,$this->proxy); //设置代理
curl_setopt($ch,CURLOPT_TIMEOUT,$this->timeout); // 置超时限制防止死循环
curl_setopt($ch,CURLOPT_AUTOREFERER,true); //自动设置Referer
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$jump); //301或302...自动跳转
curl_setopt($ch,CURLOPT_HEADER,1); //启用时会将头文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_NOBODY,false); //启用时将不对HTML中的BODY部分进行输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookieFile);
curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookieFile);
$this->headers = array_filter($this->headers,create_function( '$v ', 'return !empty($v); '));
curl_setopt($ch,CURLOPT_HTTPHEADER,$this->headers);
$str = @curl_exec($ch);
$this->info = curl_getinfo($ch);
curl_close($ch);
unset($ch);
$this->header = substr($str,0,$this->info['header_size']);
$body = substr($str,$this->info['header_size']);
$this->cookie = "";
if(preg_match_all('/Set-Cookie:([\s\S]*?)(;|[\r\n])/m',$this->header,$match))
{
foreach($match[1] as $val)
{
$this->cookie .= trim($val) . ';';
}
}
if(stripos($this->header,'Content-Encoding: gzip'))
{
if (function_exists('gzdecode'))
{
$body = gzdecode($body);
}
else
{
$body = gzinflate(substr($data,10,-8));
}
}
return $this->safeEncoding($this->header,$body,$returnCode);
}
//--POST数据格式化编码
function encodePostData($data)
{
if(is_array($data)) return http_build_query($data);
$encoded = '';
foreach(explode("&",$data) as $val)
{
$temp = explode("=",$val);
$encoded .= $encoded ? "&" : "";
$encoded .= rawurlencode($temp[0]) . "=" . rawurlencode(@$temp[1]);
}
return $encoded;
}
//---返回安全编码utf-8
function safeEncoding($header,$body_source,$code="")
{
if($code)
{
$code = $code;
}
else if(preg_match("/charset=(\S+)/i",$header,$match))
{
$code = $match[1];
}
else if(preg_match("/<meta(?:.[^>]*)charset\s*=([a-zA-Z0-9-_]+)/i",$body_source,$match))
{
$code = $match[1];
}
else
{
$code = strtolower($this->checkEncoding($body_source));
}
$code = strtolower($code);
if($code == "gb2312")
{
$code = "gbk";
}
$body = ($code == "utf-8" ? $body_source : mb_convert_encoding($body_source,'utf-8',$code));
return $body;
}
//--检查判断编码
function checkEncoding($str)
{
if($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32"))
{
return "utf-8";
}
else
{
return "gbk";
}
}
}
?>
$ch = curl_init();
$header[] = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
if(strpos($url,'https') != false){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https
$opt[CURLOPT_SSL_VERIFYHOST] = 1;
$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
}
if($header){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if(is_array($data)) $data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); //关闭curl
curl_close($ch);
return $result;
}
一个比较简单的curl POST请求的方法