PHP代碼實現抓包curl解析

背景

抓包工具charles抓取的請求curl,是這樣:php

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'linux

拿到這個curl我能夠直接在服務器跑這個curl命令,如今我想使用php作腳本,我但願能夠便利的轉換,不須要我本身寫太多代碼爬取,寫了下以下方法,後面去爬取內容兩行代碼輕鬆搞定,舒暢!android

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);json

attachments-2020-04-CAeGIjOz5e8ac53007559.jpg

相關文章
相關標籤/搜索