PHP性能分析工具XHProf深入分析
常见问题
多次调用xhprof_enable方法,最后生效的配置是哪个?
当你在一次请求中多次调用xhprof_enable方法,只有第一次调用时进行的设置能生效。在调用
xhprof_disable()后,你又可以使用xhprof_enable方法进行设置。
$i = 0;
function good(){
global $i;
$i++;
if ($i < 2) {
good();
}
}
function func() {
good();
}
$start_time = microtime(true);
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS);
xhprof_enable(XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_CPU + XHPROF_FLAGS_NO_BUILTINS);
for ($i = 0; $i < 100; $i++) {
func();
}
good();
$rst = xhprof_disable();
var_dump($rst);
输出内容为:
array(5) {
["good==>good@1"]=>
array(2) {
["ct"]=>
int(1)
["wt"]=>
int(70)
}
["func==>good"]=>
array(2) {
["ct"]=>
int(50)
["wt"]=>
int(121)
}
["main()==>func"]=>
array(2) {
["ct"]=>
int(50)
["wt"]=>
int(135)
}
["main()==>good"]=>
array(2) {
["ct"]=>
int(1)
["wt"]=>
int(0)
}
["main()"]=>
array(2) {
["ct"]=>
int(1)
["wt"]=>
int(237)
}
}
可见,打印的内容,并没有cpu和memory的信息。
输出内容中的ct,wt,cpu,mu, pmu 都代表什么意思
ct 表示 调用的次数
wt 表示 函数方法执行的时间耗时。相当于,在调用前记录一个时间,函数方法调用完毕后,计算时间差。
cpu 表示 函数方法执行消耗的cpu时间。和wt的差别在于,当进程让出cpu使用权后,将不再计算cpu时间。通过调用系统调用getrusage获取进程的占用cpu数据。
mu 表示 函数方法所使用的内存。相当于,在调用前记录一个内存占用,函数方法调用完毕后,计算内存差。调用的是zend_memory_usage获取内存占用情况。
pmu 表示 函数方法所使用的内存峰值。调用的是zend_memory_peak_usage获取内存情况。
输出内容中good==>good@1 是什么意思
==>表示一个调用关系。由于带@,说明是一个递归调用。@后面的数字是递归调用的深度。
如何设置xhprof_enable的参数,减少性能消耗
xhprof_enable提供了三个常量,用于设置你是否需要统计PHP内置函数,都统计那些指标。
三个常量如下:
XHPROF_FLAGS_NO_BUILTINS
设置这个常量后,将不统计PHP内置函数。毕竟PHP的内置函数性能一般都不错。没必要再消耗性能去统计。所以,建议设置。
XHPROF_FLAGS_CPU
设置这个常量后,会统计进程占用CPU时间。由于CPU时间是通过调用系统调用getrusage获取,导致性能比较差。开启这个选项后,大概性能下降一半。因此,如果对cpu耗时不是特别敏感的情况下,建议不要启用这个选项。
XHPROF_FLAGS_MEMORY
设置这个常量后,将会统计内存占用情况。由于获取内存情况,使用的是zend_memory_usage和zend_memory_peak_usage,并不是系统调用。因此,对性能影响不大。如果需要对内存使用情况进行分析的情况下,可以开启。
性能分析原理
如何实现对各个函数方法性能数据记录
目前xhprof会对,加载PHP文件,执行PHP函数方法,和执行eval方法进行性能数据记录。正好,这些在PHP内核中,有对应的函数进行处理。当你调用xhprof_enable方法时,会把默认的方法替换为xhprof的方法。来看看相关代码吧。
static void hp_begin(long level, long xhprof_flags)
{
if (!hp_globals.enabled)
{
int hp_profile_flag = 1;
hp_globals.enabled = 1;
hp_globals.xhprof_flags = (uint32) xhprof_flags;
/* Replace zend_compile with our proxy */
/* 处理加载PHP文件 */
/* 先把zend引擎默认处理方法保存到_zend_compile_file变量中。*/
_zend_compile_file = zend_compile_file;
/* 在把xhprof相对应的方法赋值给zend_compile_file。
这样,每次加载PHP文件时,就会执行xhprof相应的方法。*/
zend_compile_file = hp_compile_file;
/* Replace zend_compile_string with our proxy */
/* 处理eval代码的执行 */
_zend_compile_string = zend_compile_string;
zend_compile_string = hp_compile_string;
/*init the execute pointer*/
/* 处理 函数方法的执行 */
_zend_execute_ex = zend_execute_ex;
zend_execute_ex = hp_execute_ex;
.........
}
}
/*那我们看下,hp_compile_file方法,又是如何实现的*/
ZEND_DLEXPORT zend_op_array* hp_compile_file(zend_file_handle *file_handle, int type)
{
const char *filename;
char *func;
int len;
zend_op_array *ret;
int hp_profile_flag = 1;
filename = hp_get_base_filename(file_handle->filename);
len = sizeof("load") - 1 + strlen(filename) + 3;
func = (char *) emalloc(len);
snprintf(func, len, "load::%s", filename);
//方法执行前记录当前各项性能如数,如cpu 内存等
BEGIN_PROFILING(&hp_globals.entries, func, hp_profile_flag);
//开始zend引擎相应的方法,加载文件
ret = _zend_compile_file(file_handle, type);
if (hp_globals.entries)
{
//加载文件完毕后,再次记录当前各项性能数据。以便以后计算差值。
END_PROFILING(&hp_globals.entries, hp_profile_flag);
}
efree(func);
return ret;
}
xhprof在实现的时候,性能方面做了哪些优化
获取时间时,为了性能,使用了汇编来获取时间戳计数器。时间秒 = 时间戳计数器值 / CPU主频。
正是这种实现,导致目前xhprof还只适用于x86架构。此外,因为RDTSC的数据不能在CPU间同步,所以,xhprof会将进程绑定在单个CPU上。
如果SpeedStep技术是打开的,XHProf的基于RDTSC定时器的功能就不能正常工作了。这项技术在某些英特尔处理器上是可用的。[注:苹果台式机和笔记本电脑一般都将SpeedStep技术预设开启。使用XHProf,您需要禁用SpeedStep技术。 ]
inline uint64 cycle_timer()
{
uint32 __a, __d;
uint64 val;
asm volatile("rdtsc" : "=a" (__a), "=d" (__d));
(val) = ((uint64) __a) | (((uint64) __d) << 32);
return val;
}
PHP服务缓存优化原理
Nginx 根据扩展名或者过滤规则将PHP程序请求传递给解析PHP的FCGI,也就是php-fpm进程
缓存操作码(opcode)
Opcode,PHP编译后的中间文件,缓存给用户访问
当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件,该文件是执行PHP代码后的一种二进制文件表现形式。默认情况下,这个编译好的操作码文件由PHP引擎执行后丢弃;而操作码缓存的原理就是将编译后的操作码保存下来,并放入到共享内存里,以便再下一次调用该PHP页面时重用它,避免了相同代码的重复编译。节省了PHP引擎重复编译的时间,降低了服务器负载,同时减少了CPU和内存的开销.
常用的PHP缓存加速软件
1)xcache
经测试,xcache效率更好、社区活跃、兼容PHP版本多
2)ZendOpcache
Apache公司自主研发软件,5.5版本以后自带,加enbale-opcache编译参数直接使用,但是软件稳定性有待检测
3)eAccelerator
5.3版本以前经常使用的加速软件,随着5.5版本升级,和xcache等优秀软件的出现,社区活跃度开始下降
缓存软件首选xcahe、持续关注ZendOpcache...
xcache部署
1)下载xcache,添加为PHP扩展模块,编译安装
[root@web01 tools]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.bz2
[root@web01 tools]# tar xf xcache-3.2.0.tar.bz2
[root@web01 tools]# cd xcache-3.2.0
[root@web01 xcache-3.2.0]# /application/php/bin/phpize
[root@web01 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/application/php/bin/php-config
[root@web01 xcache-3.2.0]# make && make install
...
Installing shared extensions: /application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/
Installing header files: /application/php5.5.32/include/php/
2)配置php扩展生效
[root@db02 application]# vim /application/php/lib/php.ini
extension_dir = "/application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/"
extension = memcache.so
extension = imagick.so
extension = xcache.so
3)重启php后模块生效
[root@db02 application]# /application/php/bin/php -m|grep "XCache"
XCache
XCache Cacher
4)xcache配置文件
[root@db02 ~]# cat ~/tools/xcache-3.2.0/xcache.ini|egrep -v "^;|^ " >> /application/php/lib/php.ini
[xcache-common]
extension = xcache.so #模块
[xcache.admin]
xcache.admin.enable_auth = On #开启密码认证
xcache.admin.user = "mOo"
xcache.admin.pass = "md5 encrypted password"
[xcache]
xcache.shm_scheme = "mmap" #设置Xcache如何从系统分配共享内存
xcache.size = 60M #缓存大小,0禁止缓存
xcache.count = 1 #指定将xcache切分为多少块,建议与CPU核数相同(grep -c processor /proc/cpuinfo)
xcache.slots = 8K
xcache.ttl = 0 #设置cache对象生存期TTL,0永不过期;如果上线次数多,调小
xcache.gc_interval = 0 #回收器扫描过期的对象回收内存空间的间隔,0不扫描
xcache.var_size = 4M #变量缓存,而不是opcache缓存
xcache.var_count = 1
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.var_namespace_mode = 0
xcache.var_namespace = ""
xcache.coredump_type = 0
5)查看PHP chache加载情况
[root@db02 ~]# /application/php/sbin/php-fpm -v
PHP 5.5.32 (fpm-fcgi) (built: Jun 29 2016 11:32:56)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with XCache v3.2.0, Copyright (c) 2005-2014, by mOo
with XCache Cacher v3.2.0, Copyright (c) 2005-2014, by mOo
6)web界面配置
[root@db02 ~]# echo -n "123456"|md5sum
e10adc3949ba59abbe56e057f20f883e -
[root@db02 ~]# cp ~/tools/xcache-3.2.0/htdocs /application/nginx/html/www/xadmin -a
[root@db02 ~]# vim /application/php/lib/php.ini
[Date]
date.timezone = Asia/Chongqing
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"
[root@db02 ~]# pkill php-fpm
[root@db02 ~]# /application/php/sbin/php-fpm
ab压力测试效果
1)未加xcache之前
[root@db02 application]# ab -n 3000 -c 100 http://10.0.0.111/test_info.php
# 3000次会话请求、100并发数
Server Software: nginx/1.6.3
Server Hostname: 10.0.0.111
Server Port: 80
Document Path: /test_info.php #测试页面
Document Length: 83921 bytes #页面大小
Concurrency Level: 100 #100并发数
Time taken for tests: 7.973 seconds #整个测试持续时间
Complete requests: 3000 #完成的请求总数
Failed requests: 302 #失败的请求次数
(Connect: 0, Receive: 0, Length: 302, Exceptions: 0)
Write errors: 0
Total transferred: 252203675 bytes #整个过程的网络传输量
HTML transferred: 251762675 bytes #HTML内容传输量
Requests per second: 376.25 [#/sec] (mean) #吞吐量,每秒能够处理的并发数
Time per request: 265.779 [ms] (mean) #平均事务响应时间
Time per request: 2.658 [ms] (mean, across all concurrent requests)
#每个连接请求实际运行时间
Transfer rate: 30889.42 [Kbytes/sec] received
#平均每秒网络上的流量,可以帮助排除是否存在网络流量大导致响应时间延长的问题
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 3.2 0 21
Processing: 14 261 32.2 261 331
Waiting: 2 260 32.4 260 331
Total: 29 261 29.9 261 331
Percentage of the requests served within a certain time (ms)
50% 261
66% 268
75% 273
80% 276
90% 287 #90%的请求任务在287ms内完成
95% 303
98% 315
99% 322
100% 331 (longest request)
2)配置xache之后
[root@db02 application]# ab -n 3000 -c 100 http://10.0.0.111/test_info.php
Server Software: nginx/1.6.3
Server Hostname: 10.0.0.111
Server Port: 80
Document Path: /test_info.php
Document Length: 172 bytes
Concurrency Level: 100
Time taken for tests: 0.516 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Non-2xx responses: 3000
Total transferred: 969000 bytes
HTML transferred: 516000 bytes
Requests per second: 5819.42 [#/sec] (mean) #并发数上升为5000+
Time per request: 17.184 [ms] (mean)
Time per request: 0.172 [ms] (mean, across all concurrent requests)
Transfer rate: 1835.62 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.3 0 9
Processing: 6 17 2.1 16 21
Waiting: 0 17 2.2 16 21
Total: 7 17 1.6 16 21
Percentage of the requests served within a certain time (ms)
50% 16
66% 17
75% 18
80% 19
90% 19
95% 19
98% 20
99% 21
100% 21 (longest request)
由于是虚机测试环境,不一定十分准确,未安装xcache并发数在400-500,安装后并发数在5000左右,缓存效果提升10倍以上...
最近帮朋友写了一个简单的股票采集工具,采集的同花顺股票趋势信息,使用php命令行模式编写。只完成了采集部分功能,将输出的数据复制到execl(或ET)分析,比任何后台都方便。
主要函数只有一个类实现(stock.class.php):
<?php
class StockClass{
public $stockId;
public function __construct($stockId){
$this -> stockId = $stockId;
}
private function getUrl(){
return "http://stockpage.10jqka.com.cn/" . $this -> stockId . "/";
}
private function getPage(){
return file_get_contents($this -> getUrl());
}
//核心,通过正则匹配出 标签名,并将对应的方法的结果替换掉标签占位符
public function getInfo($template){
$html = $this -> getPage();
if( preg_match_all("/\{([^\}]*)\}/", $template, $result) ){
foreach($result[1] as $index => $fun){
$template = str_replace($result[0][$index], $this -> $fun($html), $template);
}
}
return mb_convert_encoding($template, "GBK", "UTF-8"); //Windows的命令提示符编码是GBK
}
private function match($pattern, $html, $itemIndex = 1){
$pattern = '/' . str_replace('/', '\/', $pattern) . '/';
if( preg_match($pattern, $html, $result) ){
return $result[$itemIndex];
}else{
return "-";
}
}
//趋势的规则都一样,合并
private function qushiPattern($name){
return '<div class="txt-aside">' . $name . ':</div>\s*<div class="txt-main">([^<]*)</div>';
}
//支持的标签
private function name($html){
return $this -> match("<title>([^\(<]*)\(", $html, 1);
}
private function score($html){
return $this -> match('<span class="analyze-num">(\d+(\.\d+)?)</span>', $html);
}
private function tips($html){
return $this -> match('<span class="analyze-tips">([^<]*)</span>', $html);
}
private function qushishort($html){
return $this -> match($this -> qushiPattern("短期趋势"), $html);
}
private function qushimiddle($html){
return $this -> match($this -> qushiPattern("中期趋势"), $html);
}
private function qushilong($html){
return $this -> match($this -> qushiPattern("长期趋势"), $html);
}
}
?>
命令提示符中的调用方法如下(stock.php):
<?php
if(count($argv) >= 2){
require("stock.class.php");
$stockId = $argv[1];
$stock = new StockClass($stockId);
$temp = $stockId;
$temp .= " {name}"; //名称
$temp .= " {score}"; //评分
$temp .= " {tips}"; //描述
$temp .= " {qushishort}"; //短期趋势
$temp .= " {qushimiddle}"; //中期趋势
$temp .= " {qushilong}"; //长期趋势
//$temp .= " {zidingyi}"; //自定义,直接在StockClass增加zidingyi方法即可
$temp .= "\n";
echo $stock -> getInfo($temp);
}
?>
直接使用 *\php.exe stock.php 股票代码即可实现调用,每次输入太长的,可以用批处理简化。将下面的代码保存为 stock.cmd。
@XXX\php.exe stock.php %1
运行结果:
这样就完成了单个股票趋势的采集,如果要采集所有的股票信息,可以保存为批处理文件(batch.cmd)
1
@echo off
call stock 000001
call stock 000002
call stock 000003
call stock 000004
call stock 000005
call stock 000006
call stock 000007
call stock 股票代码n...
双击打开即可显示,如果想保存到文件,可以执行 batch.cmd > log.txt,然后将结果复制到 Execl(或ET)即可进行更负责的分析。
前言
这次来做的是通过代码的方式,模拟登录秒拍官网,获取登录用户的信息。
前后端分离
本文的标题是CURL,传统的网站是PHP代码直接渲染项目视图,通过表单提交到控制器直接进行数据操作。
传统模拟登录的方式在页面上找到表单元素,直接进行模拟表单提交。
近两年前端技术趋于完善,实现了前后端分离。
分析页面元素
秒拍官网地址 http://www.miaopai.com/
通过审查元素以及源代码查找,在本页面并没有form标签,由此推断页面是通过js进行提交的。
查找提交地址
由于提交的js代码可能写在了外部的js文件中,直接查找很不方便,所以通过Chrome的调试模式寻找发送的http请求。
注意勾选下图的Preserve log,避免页面跳转的请求记录丢失。
填入手机号密码,在Chrome的Network中查找登录接口请求。
显然,得到登录请求接口 http://www.miaopai.com/cu/login
POSTMAN模拟登录
接口具体信息如下
首先使用postman进行模拟登录测试。
模拟登录毫无压力。
pwd字段经过测试只是简单的md5加密,没有问题。
PHP模拟登录
注意的是,postman自带方法可以直接输出各种语言的代码。
经过整理的PHP代码如下
//手机号
$phone = 13000000000;
//密码
$pwd = md5("password");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.miaopai.com/cu/login?phone={$phone}&pwd={$pwd}&checked=false&ph=0",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HEADER=>true,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
// "postman-token: c13c9f1a-fce6-7ec8-4c91-3f13bd233284"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
die();
}
//打印header
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
}
curl_close($curl);
$body_arr = (json_decode($body,true));
var_dump($body_arr);
获取用户更多数据
我们发现登录接口的返回值中,还存在url字段,拼接美拍网址之后是当前登录用户的个人页面,可以通过正则匹配的方式,得到当前登录的其他信息。
完整代码如下
//手机号
$phone = 13000000000;
//密码
$pwd = md5("password");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.miaopai.com/cu/login?phone={$phone}&pwd={$pwd}&checked=false&ph=0",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HEADER=>true,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
// "postman-token: c13c9f1a-fce6-7ec8-4c91-3f13bd233284"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
die();
}
//打印header
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
}
curl_close($curl);
$body_arr = (json_decode($body,true));
//var_dump($body_arr);
//用户个人页面
$url = "http://www.miaopai.com".$body_arr['url'];
//echo $url ;
//获取对应的数据
$http_body = curl(2,$url);
$http_body = preg_replace("/[\t\n\r]+/","",$http_body);
$str = $http_body[0];
//var_dump($str);
//解析
/*
<a title="关注" href="http://www.miaopai.com/u/mob_76195866/relation/follow.htm">5 关注</a>
*/
$reg_follow='/<a title="关注".*?>(.*?)<\/a>/i';
if(preg_match_all($reg_follow, $str, $matches)){
$body_arr['follow_num']= trim(str_replace("关注","",$matches[1][0]));
}
//粉丝
$reg_fans='/<a title="粉丝".*?>(.*?)<\/a>/i';
if(preg_match_all($reg_fans, $str, $matches)){
$body_arr['fans_num'] = trim(str_replace("粉丝","",$matches[1][0]));
}
//视频
$reg_video='/<a title="视频".*?>(.*?)<\/a>/i';
if(preg_match_all($reg_video, $str, $matches)){
$body_arr['video_num'] = strip_tags(trim(str_replace("视频","",$matches[1][0])));
}
//转发
$reg_feded='/<a title="转发".*?>(.*?)<\/a>/i';
if(preg_match_all($reg_feded, $str, $matches)){
$body_arr['fwded_num'] =strip_tags(trim(str_replace("转发","",$matches[1][0])));
}
//赞
$reg_like='/<a title="赞".*?>(.*?)<\/a>/i';
if(preg_match_all($reg_like, $str, $matches)){
$body_arr['like_num'] = strip_tags(trim(str_replace("赞","",$matches[1][0])));
}
var_dump($body_arr);
/**
* curl处理函数
* @param $url
* @param string $method
* @param array $fields
* @param array $headers
* @param bool $auth
* @return array
*/
function curl($me=1,$url, $method='GET',$fields = [], $headers=[],$auth = false){
$url=trim($url);
if($method == "GET"){
$fields_string = http_build_query($fields);
$url=$url."?".$fields_string;
}
$curl = curl_init($url);
curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, $method );
//手机
if($me == 1){
$ua="Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5";
}
//PC
if($me == 2){
$ua=-"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)";
}
curl_setopt($curl, CURLOPT_USERAGENT, $ua);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_ENCODING, "");
$header[0] = "Accept: text/html,application/xhtml+xml,application/xml;";
$header[0] .= "q=0.9,image/webp,*/*;q=0.8";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 10";
$header[] = "Accept-Encoding: gzip, deflate, sdch";
$header[] = "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($header,$headers));
if($auth){
curl_setopt($curl, CURLOPT_USERPWD, "$auth");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
if($fields){
//POST
if($method == "POST"){
$fields_string = http_build_query($fields);
if(count($fields) != 0 ){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
}
}else{
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
}
}
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header_string = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$header_rows = explode(PHP_EOL, $header_string);
foreach($header_rows as $key => $value){
$header_rows[$key]=trim($header_rows[$key]);
}
$i=0;
foreach((array)$header_rows as $hr){
$colonpos = strpos($hr, ':');
$key = $colonpos !== false ? substr($hr, 0, $colonpos) : (int)$i++;
$headers[$key] = $colonpos !== false ? trim(substr($hr, $colonpos+1)) : $hr;
}
$j=0;
foreach((array)$headers as $key => $val){
$vals = explode(';', $val);
if(count($vals) >= 2){
unset($headers[$key]);
foreach($vals as $vk => $vv){
$equalpos = strpos($vv, '=');
$vkey = $equalpos !== false ? trim(substr($vv, 0, $equalpos)) : (int)$j++;
$headers[$key][$vkey] = $equalpos !== false ? trim(substr($vv, $equalpos+1)) : $vv;
}
}
}
curl_close($curl);
return array($body, $headers);
}
得到的结果如下
后记
前后端分离是个趋势,web端也变成了无状态通信,通过token进行身份验证。
结果不重要,重在方法和思路
相关文章
Elasticsearch工具cerebro的安装与使用教程
这篇文章主要介绍了Elasticsearch工具cerebro的安装与使用教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-08- 因此,正确的原子操作是真正被执行过的。是物理执行。在当前事务中确实能看到插入的记录。最后只不过删除了。但是AUTO_INCREMENT不会应删除而改变值。1、为什么auto_increament没有回滚?因为innodb的auto_increament的...2014-05-31
- 这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
- 20岁老牌网页程序语言PHP,最快将在10月底释出PHP 7新版,这是十年来的首次大改版,最大特色是在性能上的大突破,能比前一版PHP 5快上一倍,PHP之父Rasmus Lerdorf表示,甚至能比HHVM虚拟机下的PHP程序性能更快。HHVM 是脸书为自...2015-11-24
利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化)
这篇文章主要介绍了利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化),本文给大家介绍的非常想详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-24- 这篇文章主要介绍了JavaScript提高网站性能优化的建议(二)的相关资料,需要的朋友可以参考下...2016-07-29
- 索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件中的每个列都加上索引 ...2014-06-07
国庆节到了,利用JS实现一个生成国庆风头像的小工具 详解实现过程
明天就是国庆节了,最近看到好多好友换了国庆风的头像,感觉这个挺有意思,就找到了类似的源码研究了一番,并进行了改造(并非原创,只是进行了改造,只要想分享一下实现思路)。下面就来看看如何实现一键生成国庆风头像小工具。​...2021-10-01- 这篇文章主要介绍了市面上短视频(douyin)"去水印"的工具原来是这样实现的,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-30
- 这篇文章主要介绍了提升jQuery的性能需要做好的七件事,希望真的帮助大家提升jQuery性能,需要的朋友可以参考下...2016-01-14
- 本文主要讲述了利用Python网络爬虫对指定京东商城中指定商品下的用户评论进行爬取,对数据预处理操作后进行文本情感分析,感兴趣的朋友可以了解下...2021-05-28
- Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。这篇文章主要介绍了underscore源码分析相关知识,感兴趣的朋友一起学习吧...2016-01-02
- Google是这样介绍PageRank的: Google 出类拔萃的地方在于专注开发“完美的搜索引擎”,联合创始人拉里·佩奇将这种搜索引擎定义为可“确解用户...2017-07-06
Eclipse中安装反编译工具Fernflower的方法(Enhanced Class Decompiler)
这篇文章主要介绍了Eclipse中安装反编译工具Fernflower的方法(Enhanced Class Decompiler),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-20Fatal error: Cannot redeclare class 原因分析与解决办法
我使用的都是php __autoload状态自动加载类的,今天好好的程序不知道怎么在运行时提示Fatal error: Cannot redeclare class 了,看是重复定义了类,下面我来分析一下解决办...2016-11-25- php测试性能代码 function microtime_float () { list ($usec, $sec) = explode(" ", microtime()); return ((float) $usec + (float) $sec); } functio...2016-11-25
- 本文主要介绍了Vue组件文档生成工具库的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-11
- 本文给大家分享的时间戳与时间相互转换(php .net精确到毫秒) ,感兴趣的朋友一起学习吧...2020-06-25
- 文章介绍了css中空路径对页面性能影响的解决方案,这个可能很多美工朋友不会去注意这一点,下面我们来看看吧。 在写 CSS 的时候,用 background:url(#) 还是会对页面进...2017-07-06
- 这篇文章主要介绍了如何用Node.js编写内存效率高的应用程序,对Node.js感兴趣的同学,可以参考下...2021-05-01