天天看點

curl http header_PHP代碼實作抓包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);
           
以上内容希望幫助到大家,

很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那裡入手去提升,對此我整理了一些資料,包括但不限于:分布式架構、高可擴充、高性能、高并發、伺服器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點進階進階幹貨需要的可以免費分享給大家

,需要

PHP進階架構師>>>視訊、面試文檔免費擷取​docs.qq.com

curl http header_PHP代碼實作抓包curl解析
或 者關注咱們下面的知乎專欄

PHP架構師圈子​zhuanlan.zhihu.com

curl http header_PHP代碼實作抓包curl解析
來源:https://www.cnblogs.com/followyou/p/11878778.html
           

繼續閱讀