page contents

PHP代码实现抓包curl解析

以下内容希望帮助到大家!

attachments-2020-04-ODV7AR8b5e8ac5011e171.jpg

背景


抓包工具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);



attachments-2020-04-CAeGIjOz5e8ac53007559.jpg

  • 发表于 2020-04-06 13:58
  • 阅读 ( 637 )
  • 分类:PHP开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1470 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章