今天是:2025年4月9日 星期三
记住用户名密码
工作中需要对接调试http接口,之前都是写代码测试,发现直接用curl这个工具更简单高效。
举例如下:
想要post一包数据给这个接口https://XXXXXXXXX
http报文头规定如下:
1 2 3 4 5 6 7 8 | POST / HTTP/1.1 Host: XXXXXXXXXX:XXXX Accept: */* User-Agent: Donjin Http 0.1 Content-Type: x-ISO-TPDU/x-auth Cache-Control: no-cache Content-Length: 93 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(hex data) |
用curl测试:其中需要提交的二进制数据,为了方便,写到aaa.bin文件中了。
1 2 | curl https: //XXXXXXXXX -v --cacert ./UP.pem -k -H 'User-Agent: Donjin Http 0.1' \ -H 'Content-Type: x-ISO-TPDU/x-auth' -H 'Cache-Control: no-cache' -H 'Content-Length: 93' --data-binary @aaa.bin |
相比之前,用php写了个测试demo来说,这个工具更简单高效。
之前的php demo如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | <?php echo "hello test 111!" ; echo "<br>" ; /* PHP CURL HTTPS POST */ function curl_post_https( $url , $data ){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_setopt( $curl , CURLOPT_URL, $url ); // 要访问的地址 curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在 curl_setopt( $curl , CURLOPT_USERAGENT, $_SERVER [ 'HTTP_USER_AGENT' ]); // 模拟用户使用的浏览器 curl_setopt( $curl , CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt( $curl , CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt( $curl , CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt( $curl , CURLOPT_POSTFIELDS, $data ); // Post提交的数据包 curl_setopt( $curl , CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt( $curl , CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec( $curl ); // 执行操作 if (curl_errno( $curl )) { echo 'Errno' .curl_error( $curl ); //捕抓异常 } curl_close( $curl ); // 关闭CURL会话 return $tmpInfo ; // 返回数据,json格式 } //echo getcwd(); /** * @name ssl Curl Post数据 * @param string $url 接收数据的api * @param string $vars 提交的数据 * @param int $second 要求程序必须在$second秒内完成,负责到$second秒后放到后台执行 * @return string or boolean 成功且对方有返回值则返回 */ function curl_post_ssl( $url , $data , $second =30, $aHeader = array ()) { $ch = curl_init(); curl_setopt( $ch ,CURLOPT_VERBOSE, '1' ); curl_setopt( $ch ,CURLOPT_TIMEOUT, $second ); //curl_setopt($ch,CURLOPT_VERBOSE, '1'); //debug模式,方便出错调试 curl_setopt( $ch ,CURLOPT_RETURNTRANSFER, 1); curl_setopt( $ch ,CURLOPT_URL, $url ); curl_setopt( $ch ,CURLOPT_SSL_VERIFYPEER,false); curl_setopt( $ch ,CURLOPT_SSL_VERIFYHOST,false); //curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert/CUP_cacert.pem'); // curl_setopt($ch,CURLOPT_SSLCERTPASSWD,''); curl_setopt( $ch ,CURLOPT_SSLKEYTYPE, 'PEM' ); curl_setopt( $ch ,CURLOPT_SSLKEY, getcwd (). '/cert/CUP_cacert.pem' ); //设置header信息 curl_setopt( $ch , CURLOPT_HTTPHEADER, array ( 'User-Agent: Donjin Http 0.1' , 'Content-Type: x-ISO-TPDU/x-auth' , 'Cache-Control: no-cache' , 'Content-Length: ' . strlen ( $data )) ); if ( count ( $aHeader ) >= 1 ){ curl_setopt( $ch , CURLOPT_HTTPHEADER, $aHeader ); } curl_setopt( $ch ,CURLOPT_POST, 1); curl_setopt( $ch ,CURLOPT_POSTFIELDS, $data ); $data = curl_exec( $ch ); //$curlInfo = curl_getinfo($ch); //echo $curlInfo; if (curl_errno( $ch )) { echo 'Errno' .curl_error( $ch ); //捕抓异常 } curl_close( $ch ); if ( $data ) return $data ; else return false; } /* $data = [0,1]; $aHeader =[]; $rcode = curl_post_ssl($url,$data,10,$aHeader); if($rcode) { echo "ok"! } else { echo "error"! } */ function hex2bin( $h ){ if (! is_string ( $h )) return null; $r = '' ; for ( $a =0; $a < strlen ( $h ); $a +=2) { $r .= chr (hexdec( $h { $a }. $h {( $a +1)})); } return $r ; } function Hex2String( $hex ){ $string = '' ; for ( $i =0; $i < strlen ( $hex )-1; $i +=2){ $string .= chr (hexdec( $hex [ $i ]. $hex [ $i +1])); } return $string ; } function hextostr( $hex ) { return preg_replace_callback( '/\\\x([0-9a-fA-F]{2})/' , function ( $matches ) { return chr (hexdec( $matches [1])); }, $hex ); } $string = "005BXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0303135393838303231303031303231303136300011000000010030002953657175656e6365204e6f3136333135305358582d34433330343131390003303120" ; $data = hex2bin( $string ); #var_dump( $data ); $url = 'https://XXXXXXXXXXXXXXXXX' ; $aHeader = array (); $rcode = curl_post_ssl( $url , $data ,10, $aHeader ); //$rcode = curl_post_https($url,$data); if ( $rcode ) { echo "ok" ; echo "<br>" ; echo $rcode ; //$myStr=""; //for($i=0;isset($rcode[$i]);$i++) //{ // $myStr.= chr($rcode[$i]); //} //var_dump($myStr); //$res=json_decode($rcode,true); echo "<br>" ; echo 'respond hex data:' ; echo "<br>" ; $arr1 = str_split ( $rcode , 1); foreach ( $arr1 as $akey => $aval ){ $arr1 [ $akey ]= " " .bin2hex( $aval ); echo $arr1 [ $akey ]; } //var_dump($arr1); //echo hextostr($rcode); //var_dump($rcode); //echo Hex2String($rcode); //implode('!', $rcode); } else { echo "error" ; echo "<br>" ; } |
目前有 0 条留言 其中:访客:0 条, 博主:0 条