php curl 登录接口测试 token写入token.txt 提取http header

* login.php

 '13001919???',
	// "username" => 'nosuchuser',
    "userpwd" => 'putongj3',
	'luckdrawcode' => '',
	'specialcode' =>'undefined'
];
$postdata = build_query($a);

array_push($headers, sprintf("Content-Length: %d", strlen($postdata)));

curl_setopt_array($ch, [
    // CURLOPT_URL => 'http://172.16.0.224:8060/v1/login',
	CURLOPT_URL => 'http://192.168.4.157:8060/v1/login',
    CURLOPT_HEADER => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_BINARYTRANSFER => 1,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $postdata,
	CURLOPT_CONNECTTIMEOUT => 3,
]);

$data = curl_exec($ch);

if (curl_errno($ch)) {
	$msg = curl_error($ch);
	printf("%s\n", $msg);
} else {
	printf("%s\n", $data);
	
	// write token  o.data.token
    $body = extract_http_body($data, "\r\n\r\n");
    // printf("body=[%s]\n", $body);
    $json = json_decode($body, false);
    if ($json) {
        file_put_contents("token.txt", $json->data->token);
    }

}
curl_close($ch);

* run:

$ php login.php

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache, no-store, max-age=0, must-revalidate, value
Content-Type: application/json; charset=utf-8
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified: Tue, 05 Mar 2019 08:03:51 GMT
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Request-Id: 1af4693d-4b92-400b-8e85-f2b550529e61
X-Xss-Protection: 1; mode=block
Date: Tue, 05 Mar 2019 08:03:51 GMT
Content-Length: 221

{"code":0,"message":"OK","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTE3NzMwMzEsImlkIjoyOTY1MTYwNywibmJmIjoxNTUxNzczMDMxLCJ1c2VybmFtZSI6InB1dG9uZ2ozIn0.z1pzIFQ1FUF-8W2ZX9aDWbMjL9BOv5CHy1vKLvKKBeQ"}}

$ cat token.txt

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTE3NzMwMzEsImlkIjoyOTY1MTYwNywibmJmIjoxNTUxNzczMDMxLCJ1c2VybmFtZSI6InB1dG9uZ2ozIn0.z1pzIFQ1FUF-8W2ZX9aDWbMjL9BOv5CHy1vKLvKKBeQ

 

2. 提取httpheader

* indexOf.php


301 Moved Permanently

301 Moved Permanently


nginx
\r\n EOF; function extractHttpHeader($s, $delim = "\r\n\r\n") { $n = strlen($delim); $j = 0; for ($i = 0; isset($s[$i]) && $j < $n; $i++) { if ($delim[$j] === $s[$i]) { $j++; } else { $j = 0; } } return substr($s, 0, $i-$n); } function getHeaderAssoc($header) { $a = explode("\r\n", $header); $assoc = []; for ($i = 1; $i < count($a); $i++) { $b = explode(": ", $a[$i]); $assoc[ $b[0] ] = $b[1]; } return $assoc; } $header = extractHttpHeader($s); $assoc = getHeaderAssoc($header); var_dump($assoc);

运行效果如下:

$ php indexOf.php
array(8) {
  ["Date"]=>
  string(29) "Fri, 30 Aug 2019 06:41:52 GMT"
  ["Content-Type"]=>
  string(9) "text/html"
  ["Content-Length"]=>
  string(3) "178"
  ["Connection"]=>
  string(10) "keep-alive"
  ["Location"]=>
  string(24) "https://www.kancloud.cn/"
  ["X-Via-JSL"]=>
  string(9) "0b3d685,-"
  ["Set-Cookie"]=>
  string(79) "__jsluid_h=6ef203be13858b5f3d31576bb41f4640; max-age=31536000; path=/; HttpOnly"
  ["X-Cache"]=>
  string(5) "error"
}

 

--------------------------------------------------------------

windows share:

sudo mount -t cifs //ET-201808271128/gocode/src/gitlab.easytech-main.com/webdev/ucenter/src /mnt -o username=mingzhanghui,password=XXX,vers=2.0,rw,uid=1000,gid=1000,dir_mode=0777,file_mode=0777

你可能感兴趣的:(php,web,curl,php,token,post,login)