zoukankan      html  css  js  c++  java
  • 抓包curl解析

    背景

    抓包工具charles抓取的请求curl,是这样:

    curl -H ':method: POST' -H ':path: /client.action?functionId=signInCouponCenter&clientVersion=8.3.4&build=70302&client=android&d_brand=HUAWEI&d_model=JKM-AL00bxxxxx' -H ':authority: api.m.jd.com' -H ':scheme: https' -H 'cookie:xxxxx' -H 'charset: UTF-8' -H 'accept-encoding: gzip,deflate'  -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'content-length: 95' -H 'user-agent: okhttp/3.12.1' --data-binary "body=%22%7D&" 'https://api.m.jd.com/client.action?functionId=signInCouponCenter&clientVersion=8.3.4&build=70302&client=android&d_brand=HUAWEI&d_model=JKM-AL00bxxx'
    

    拿到这个curl我可以直接在服务器跑这个curl命令,现在我想使用php做脚本,我希望可以便利的转换,不需要我自己写太多代码爬取,写了下如下方法,后面去爬取内容两行代码轻松搞定,舒畅!

    code

    <?php
    
      //linux curl 解析
        public function curlParse( $curls ){
    
            $curls = trim($curls,'curl');
            $h = explode(' -H ',$curls);
            $h = array_filter($h);
            $data = array_pop($h);
            $d = explode(' --data-binary ',$data);
            $h[] = array_shift($d);
    
            $header = [];
            $actions = [];
            foreach ($h as $k=>$v){
                $v = trim($v,"'");
                $t = explode(' ',$v);
                $key = array_shift($t);
                if( in_array($key,[':path:',':method:','authority','scheme']) ){
                    $actions[trim($key,':')] = implode(' ',$t);
                    unset($h[$k]);
                }
    
                $header[trim($key,':')] = implode(' ',$t);
            }
    
            $d = explode(' ',array_pop($d));
            $submitData = trim($d[0],""");
            $url = trim(array_pop($d),"'");
    
            $method = $actions['method'];
            return httpRequest($url,$submitData,$header,$method);
        }
    
    
        //请求
        public function httpRequest($url,$data,$header=[],$method){
    
            if ( empty($header[0]) ) {
                $headers = [];
               foreach ($header as $k => $v){
                   $headers[] = "{$k}: {$v}";
               }
                $header = $headers;
            }
    
            $curl = curl_init();
    
            $curlSet = [
                    CURLOPT_URL => $url,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_HTTPHEADER => $header
            ];
    
            if( $method=='POST' ){
                $curlSet[CURLOPT_CUSTOMREQUEST] = "POST";
                $curlSet[CURLOPT_POSTFIELDS] = $data;
            }
    
            curl_setopt_array($curl,$curlSet);
    
            $response = curl_exec($curl);
            $err = curl_error($curl);
    
            curl_close($curl);
    
            if ($err)
                throw new Exception("cURL Error #:" . $err);
    
            $response = json_decode($response,1);
            return $response;
        }
        
        $curl = "curl -H ':method: POST' -H ':path: /client.action?functionId=signInCouponCenter&clientVersion=8.3.4&build=70302&client=android&d_brand=HUAWEI&d_model=JKM-AL00bxxxxx' -H ':authority: api.m.jd.com' -H ':scheme: https' -H 'cookie:xxxxx' -H 'charset: UTF-8' -H 'accept-encoding: gzip,deflate'  -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'content-length: 95' -H 'user-agent: okhttp/3.12.1' --data-binary "body=xxx" 'https://api.m.jd.com/client.action?functionId=signInCouponCenter&clientVersion=8.3.4&build=70302&client=android&d_brand=HUAWEI&d_model=JKM-AL00bxxx'";
        curlParse($curl);
        
    
  • 相关阅读:
    jstree 的使用(增 删 改 查)
    关于Random 和 List<int>的Exist的方法使用
    编译器错误消息: CS0016: 未能写入输出文件“c:WindowsMicrosoft.NETFramework64v4.0.30319Temporary ASP.NET Files oot1b124ac2fb984fcApp_global.asax.vbtifrrw.dll”--“拒绝访问。 ”
    SQL Server执行动态SQL正确方式
    C#设计模式——简单说(简单工厂模式)
    c#设计模式——简单说(建造者模式)
    nginx将http请求转发到https
    maven出现:Failed to execute goal on project ...: Could not resolve dependencies for project ...
    动态切换element-ui表格中每行的icon
    axios拦截token过期,返回401的情况
  • 原文地址:https://www.cnblogs.com/followyou/p/11878778.html
Copyright © 2011-2022 走看看