标签 PHP网络请求 下的文章 - 易航博客
首页
统计
友链
4K壁纸
留言板
关于
推荐
百度一下: 易航博客
搜 索
1
Joe主题再续前缘版 - 本站同款
5,733 阅读
2
易航网址引导系统
3,070 阅读
3
Js自动播放HTML音乐(不受浏览器限制,无需先与浏览器交互,无需对浏览器进行修改)
1,982 阅读
4
六个好看实用的HTML登录界面源码
1,769 阅读
5
V免签全开源免签约码支付系统(支持:支付宝 微信 QQ)
716 阅读
源码大全
PHP源码
HTML模板
Java源码
小程序源码
热门程序
Typecho
ThinkPHP
Joe主题
技术教程
HTML教程
PHP教程
CSS教程
JS教程
MySQL教程
SEO教程
正则表达式教程
网络技术
话题讨论
PHP话题
书虫小说
剑来
雪中悍刀行
值得一看
媒体新闻
人生语录
热门事件
活动线报
经验分享
关于我们
站点公告
其他文章
登录
/
注册
搜 索
标签搜索
网站源码
PHP
PHP教程
PHP源码
PHP函数方法
JavaScript
HTML模板
HTML
Joe主题
网站模板
Typecho
ThinkPHP
PHP网络请求
支付系统
通信协议
码支付
API接口
Typecho插件
PHP发送数据
网络攻击
易航
累计撰写
90
篇文章
累计收到
920
条评论
首页
栏目
源码大全
PHP源码
HTML模板
Java源码
小程序源码
热门程序
Typecho
ThinkPHP
Joe主题
技术教程
HTML教程
PHP教程
CSS教程
JS教程
MySQL教程
SEO教程
正则表达式教程
网络技术
话题讨论
PHP话题
书虫小说
剑来
雪中悍刀行
值得一看
媒体新闻
人生语录
热门事件
活动线报
经验分享
关于我们
站点公告
其他文章
页面
统计
友链
4K壁纸
留言板
关于
推荐
百度一下: 易航博客
用户登录
登录
注册
找到
4
篇与
PHP网络请求
相关的结果
2022-08-13
PHP获取网站标题、关键词与描述
PHP如何获取网站标题、关键词与描述呢?在网页采集过程中,我们需要获取一个网站的meta信息,如title、keywords、description等,但是如果用普通的正则匹配很容易出错。那么到底该如何写这个PHP的代码,这篇文章为你带来解决方法。使用get_meta_tags函数获取meta信息比如我们要获取 http://blog.bri6.cn 这个网页的meta信息,可以直接使用php内置函数get_meta_tags获取,代码如下:<?php $meta_tags = get_meta_tags("http://blog.bri6.cn/"); print_r($meta_tags); ?>运行结果:你会发现获取了网页的关键词与描述,但是发现缺少了网页的标题,原因是标题并不是meta标签,而是组成的,所以我们的完整代码应该如下:/** 获取META信息 */ function get_sitemeta($url) { $data = file_get_contents($url); $meta = array(); if (!empty($data)) { #Title preg_match('/<TITLE>([\w\W]*?)<\/TITLE>/si', $data, $matches); if (!empty($matches[1])) { $meta['title'] = $matches[1]; } #Keywords preg_match('/<META\s+name="keywords"\s+content="([\w\W]*?)"/si', $data, $matches); if (empty($matches[1])) { preg_match("/<META\s+name='keywords'\s+content='([\w\W]*?)'/si", $data, $matches); } if (empty($matches[1])) { preg_match('/<META\s+content="([\w\W]*?)"\s+name="keywords"/si', $data, $matches); } if (empty($matches[1])) { preg_match('/<META\s+http-equiv="keywords"\s+content="([\w\W]*?)"/si', $data, $matches); } if (!empty($matches[1])) { $meta['keywords'] = $matches[1]; } #Description preg_match('/<META\s+name="description"\s+content="([\w\W]*?)"/si', $data, $matches); if (empty($matches[1])) { preg_match("/<META\s+name='description'\s+content='([\w\W]*?)'/si", $data, $matches); } if (empty($matches[1])) { preg_match('/<META\s+content="([\w\W]*?)"\s+name="description"/si', $data, $matches); } if (empty($matches[1])) { preg_match('/<META\s+http-equiv="description"\s+content="([\w\W]*?)"/si', $data, $matches); } if (!empty($matches[1])) { $meta['description'] = $matches[1]; } } return $meta; }
2022年08月13日
99 阅读
0 评论
0 点赞
2022-08-12
PHP万能Curl PHP完整实用Curl函数
PHP curl功能函数,请求网站那必须用curl,curl方法速度最快这个curl函数功能非常全面了,可以请求99%的网站。function curl($url, $paras = []) { $ch = curl_init(); if (isset($paras['Header'])) { $Header = $paras['Header']; } else { $Header[] = "Accept:*/*"; $Header[] = "Accept-Encoding:gzip,deflate,sdch"; $Header[] = "Accept-Language:zh-CN,zh;q=0.8"; $Header[] = "Connection:close"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $Header); if (isset($paras['ctime'])) { // 连接超时 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']); } else { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); } if (isset($paras['rtime'])) { // 读取超时 curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']); } if (isset($paras['post'])) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']); } if (is_array($paras['get'])) { $paras['get'] = http_build_query($paras['get']); $url = strstr($url, '?') ? trim($url, '&') . '&' . $paras['get'] : $url . '?' . $paras['get']; } if (isset($paras['header'])) { curl_setopt($ch, CURLOPT_HEADER, true); } if (isset($paras['cookie'])) { curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']); } if (isset($paras['refer'])) { if ($paras['refer'] == 1) { curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f='); } else { curl_setopt($ch, CURLOPT_REFERER, $paras['refer']); } } if (isset($paras['ua'])) { curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']); } else { curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"); } if (isset($paras['nobody'])) { curl_setopt($ch, CURLOPT_NOBODY, 1); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if (isset($paras['GetCookie'])) { curl_setopt($ch, CURLOPT_HEADER, 1); $result = curl_exec($ch); preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($result, 0, $headerSize); //状态码 $body = substr($result, $headerSize); $ret = [ "Cookie" => $matches, "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), ]; curl_close($ch); return $ret; } $ret = curl_exec($ch); if (isset($paras['loadurl'])) { $Headers = curl_getinfo($ch); if (isset($Headers['redirect_url'])) { $ret = $Headers['redirect_url']; } else { $ret = false; } } curl_close($ch); return $ret; }使用方法GET访问curl('http://blog.bri6.cn');GET携带参数访问curl('http://blog.bri6.cn', [ 'get' => [ 'url' => 'blog.bri6.cn' ] ]);POST访问curl('http://blog.bri6.cn', [ 'post' => [ 'url' => 'blog.bri6.cn' ] ]);或者curl('http://blog.bri6.cn', [ 'post' => 'url=blog.bri6.cn' ]);带Cookiecurl('http://blog.bri6.cn', [ 'cookie' => 'cookie内容' ]);模拟refercurl('http://blog.bri6.cn', [ 'refer' => 'https://xxx' ]);模拟UAcurl('http://blog.bri6.cn', [ 'ua' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' ]);上传文件curl('http://blog.bri6.cn', [ 'post' => [ 'file' => new CURLFile(realpath("Curl.jpg")) ] ]);或者curl('http://blog.bri6.cn', [ 'post' => new CURLFile(realpath("Curl.jpg")) ]);获取301地址curl('http://blog.bri6.cn', [ 'loadurl' => 1 ]);返回Header信息curl('http://blog.bri6.cn', [ 'header' => 1 ]);设置请求头curl('http://blog.bri6.cn', [ 'Header' => [ 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 accept-encoding: gzip, deflate, br accept-language: zh-CN,zh;q=0.9 cache-control: max-age=0' ] ]);获取请求的全部信息curl('http://blog.bri6.cn', [ 'post' => [ 'user' => 123456, 'pwd' => 123 ], 'GetCookie' => 1 ]);
2022年08月12日
51 阅读
0 评论
0 点赞
2022-06-28
PHP使用Curl函数进行远程请求案例,爬虫,可保存账户登录状态
CURL简介:CURL可以使用URL的语法模拟浏览器来传输数据,因为它是模拟浏览器,因此它同样支持多种协议,FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP等协议都可以很好的支持,包括一些:HTTPS认证,HTTP POST方法,HTTP PUT方法,FTP上传,keyberos认证,HTTP上传,代理服务器,cookies,用户名/密码认证,下载文件断点续传,上传文件断点续传,http代理服务器管道,甚至它还支持IPv6,scoket5代理服务器,通过http代理服务器上传文件到FTP服务器等等。本文主要介绍的是php远程请求CURL(爬虫、保存登录状态)的相关内容,下面话不多说了,来一起看看详细的介绍吧GET案例:<?php /** * curl_get * @param $url * @param null $param * @param null $options * @return array */ function curl_get($url, $param = null, $options = null) { if (empty($options)) { $options = array( 'timeout' => 30, // 请求超时 'header' => array(), // 数据格式如array('Accept: */*','Accept-Encoding: gzip, deflate, br') 'cookie' => '', // cookie字符串,浏览器直接复制即可 'cookie_file' => '', // 文件路径,并要有读写权限的 'ssl' => 0, // 是否检查https协议 'referer' => null ); } else { empty($options['timeout']) && $options['timeout'] = 30; empty($options['ssl']) && $options['ssl'] = 0; } $result = array( 'code' => 0, 'msg' => 'success', 'body' => '' ); if (is_array($param)) { $param = http_build_query($param); } $url = strstr($url, '?') ? trim($url, '&') . '&' . $param : $url . '?' . $param; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // 设置url !empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 if (!empty($options['cookie_file']) && file_exists($options['cookie_file'])) { curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); } else if (!empty($options['cookie'])) { curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); } curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 curl_setopt($ch, CURLOPT_HEADER, 1); // 获取请求头 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 输出转移,不输出页面 !$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl !empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']); //伪装请求来源,绕过防盗 curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); //执行并获取内容 $output = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($output, 0, $header_size); $output = substr($output, $header_size); //对获取到的内容进行操作 if ($output === FALSE) { $result['code'] = 1; // 错误 $result['msg'] = "CURL Error:" . curl_error($ch); } $result['header'] = $header; $result['body'] = $output; //释放curl句柄 curl_close($ch); return $result; } ?>POST案例:<?php /** * curl_post * @param $url 请求地址 * @param null $param post参数 * @param array $options 配置参数 * @return array */ function curl_post($url, $param = null, $options = array()) { if (empty($options)) { $options = array( 'timeout' => 30, 'header' => array(), 'cookie' => '', 'cookie_file' => '', 'ssl' => 0, 'referer' => null ); } else { empty($options['timeout']) && $options['timeout'] = 30; empty($options['ssl']) && $options['ssl'] = 0; } $result = array( 'code' => 0, 'msg' => 'success', 'body' => '' ); if (is_array($param)) { $param = http_build_query($param); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // 设置url !empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 if (!empty($options['cookie_file']) && file_exists($options['cookie_file'])) { curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); } else if (!empty($options['cookie'])) { curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); } curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); curl_setopt($ch, CURLOPT_HEADER, 1); // 获取请求头 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 输出转移,不输出页面 !$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl !empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']); //伪装请求来源,绕过防盗 curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); //执行并获取内容 $output = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($output, 0, $header_size); $output = substr($output, $header_size); //对获取到的内容进行操作 if ($output === FALSE) { $result['code'] = 1; // 错误 $result['msg'] = "CURL Error:" . curl_error($ch); } $result['header'] = $header; $result['body'] = $output; //释放curl句柄 curl_close($ch); return $result; } ?>关于php远程请求CURL(爬虫,保存登录状态)的这篇文章到此结束。其他请求类型行请参考这篇文章:PHP进行各种网络请求的方式和实现函数总结
2022年06月28日
57 阅读
0 评论
0 点赞
2022-06-28
PHP进行各种网络请求的方式和实现函数总结
首先,分析php发送网络请求的方法对于php发送网络请求,我们最常用的请求是curl。有时候我们也会使用file_get_contents函数来发送网络请求,但是file_get_contents只能完成一些间接的网络请求,稍微复杂一点的是无法完成的,比如文件上传、cookies、验证、表单提交等。php中的Curl可以使用url的语法来模拟浏览器传输数据。因为是模拟浏览器,所以也支持多种协议。FTP,FTPS,http,httpS,Gopher,Telnet,Dict,File,LDAP都可以很好的支持,包括一些:HTTPS认证,HTTP POST方法,Put方法,FTP上传,keyberos认证,HTTP上传,代理服务器,cookies,用户名/密码认证,下载文件断点续传,上传文件断点续传,HTTP代理服务器管道,甚至它支持IPv6,scoket5代理服务器,通过HTTP代理服务器上传文件到FTP服务器等。,所以我们在开发中尽量使用curl做网络请求,不管是简单的还是复杂的。二、file_get_contents发送网络请求示例file_get_contents(path,include_path,context,start,max_length);参数描述path必需。规定要读取的文件。include_path可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。context可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。若使用 null,则忽略。start可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。max_length可选。规定读取的字节数。该参数是 PHP 5.1 新加的。一般用file_get_contents或者fopen, file , readfile等函数读取url的时候 会创建一个$http_response_header变量保存HTTP响应的报头,使用fopen等函数打开的数据流信息可以用stream_get_meta_data获取$html = file_get_contents('http://www.baidu.com'); print_r($http_response_header); $fp = fopen('http://www.baidu.com', 'r'); print_r(stream_get_meta_data($fp)); fclose($fp);摸拟post请求:$url = 'http://192.168.1.1/test.php'; $data = array( 'keyword' => 'test data', ); $content = http_build_query($data); $content_length = strlen($content); $options = array( 'http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: $content_length\r\n", 'content' => $content ) ); echo file_get_contents($url, false, stream_context_create($options));三、PHP通过curl方法发送网络请求curl可以支持https认证、http post、ftp上传、代理、cookies、简单口令认证等等功能,使用前需要先在你的PHP环境中安装和启用curl模块,这里有两种写法供大家参考:<?php function geturl($url){ $headerArray =array("Content-type:application/json;","Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output,true); return $output; } function posturl($url,$data){ $data = json_encode($data); $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json"); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return json_decode($output,true); } function puturl($url,$data){ $data = json_encode($data); $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串 $output = curl_exec($ch); curl_close($ch); return json_decode($output,true); } function delurl($url,$data){ $data = json_encode($data); $ch = curl_init(); curl_setopt ($ch,CURLOPT_URL,$put_url); curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output,true); } function patchurl($url,$data){ $data = json_encode($data); $ch = curl_init(); curl_setopt ($ch,CURLOPT_URL,$url); curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); //20170611修改接口,用/id的方式传递,直接写在url中了 $output = curl_exec($ch); curl_close($ch); $output = json_decode($output); return $output; } ?>一个函数片时各种请求:function sendCurl($url, $data = null,$method='POST') { $method=strtoupper($method); $start_wdmcurl_time = microtime(true); $header = array(' application/x-www-form-urlencoded'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); // https 请求 if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if($method=='GET'){ if($data && is_array($data) && count($data)>0 ){ $url.="?".http_build_query($data); } curl_setopt($ch, CURLOPT_URL, $url); }elseif($method=='POST'){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); if (is_array($data) && count($data)>0) { curl_setopt($ch, CURLOPT_POST, true); $isPostMultipart = false; foreach ($data as $k => $v) { if ('@' == substr($v, 0, 1)) { $isPostMultipart = true; break; } } unset($k, $v); if ($isPostMultipart) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } } }elseif(in_array($method,['PUT','DELETE','PATCH'])){ curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$method); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$header); $reponse = curl_exec($ch); curl_close($ch); return $reponse; }四、使用PHP Composer的扩展库guzzlehttpcomposer require guzzlehttp/guzzle$client = new \GuzzleHttp\Client(); $response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle'); echo $response->getStatusCode(); // 200 echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8' echo $response->getBody(); // '' // Send an asynchronous request. $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org'); $promise = $client->sendAsync($request)->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); $promise->wait();平常开发中尽量使用方法三,自定义curl处理网络请求,或者composer的guzzlehttp扩展库,用起来也很方便。
2022年06月28日
63 阅读
0 评论
2 点赞