问题遇到的现象和发生背景
php culr 和js请求 差不多代码返回的值不一样
问题相关代码,请勿粘贴截图
js 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
var urlencoded = new URLSearchParams();
urlencoded.append("code", "4%2F0AdQt8qgsjl6uJFX8yrAhfuqgrn_veSlnC4Y2X06cGYXDkb4xXfWpX6g6dE8RPQEp9nsu8w");
urlencoded.append("client_id", "****");
urlencoded.append("client_secret", "*****");
urlencoded.append("redirect_uri", "https%3A%2F%2Fwww.googleapis.com%2Foauth2%2Fv4%2Ftoken");
urlencoded.append("grant_type", "authorization_code");
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded,
redirect: 'follow'
};
fetch("https://oauth2.googleapis.com/token", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
</body>
</html>
php 代码
$curl = curl_init();
$data = 'code=4%252F0AdQt8qgsjl6uJFX8yrAhfuqgrn_veSlnC4Y2X06cGYXDkb4xXfWpX6g6dE8RPQEp9nsu8w&client_id=*****&client_secret=*******&redirect_uri=https%253A%252F%252Fwww.googleapis.com%252Foauth2%252Fv4%252Ftoken&grant_type=authorization_code';
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://oauth2.googleapis.com/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . mb_strlen($data),
'Host:oauth2.googleapis.com',
),
));
$response = curl_exec($curl);
$curl_info = curl_getinfo($curl);
var_dump($curl_info);
var_dump($response);
exit;
运行结果及报错内容
js返回
{
"error": "invalid_request",
"error_description": "\nYou can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure.\n\nYou can let the app developer know that this app doesn't comply with one or more Google validation rules.\n "
}
php 返回
array(27) {
["url"]=>
string(35) "https://oauth2.googleapis.com/token"
["content_type"]=>
string(10) "text/plain"
["http_code"]=>
int(404)
["header_size"]=>
int(128)
["request_size"]=>
int(460)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(18)
["redirect_count"]=>
int(0)
["total_time"]=>
float(1.794538)
["namelookup_time"]=>
float(0.004638)
["connect_time"]=>
float(1.12329)
["pretransfer_time"]=>
float(1.705011)
["size_upload"]=>
float(325)
["size_download"]=>
float(18)
["speed_download"]=>
float(10)
["speed_upload"]=>
float(181)
["download_content_length"]=>
float(18)
["upload_content_length"]=>
float(325)
["starttransfer_time"]=>
float(1.79451)
["redirect_time"]=>
float(0)
["redirect_url"]=>
string(0) ""
["primary_ip"]=>
string(14) "43.245.222.173"
["certinfo"]=>
array(0) {
}
["primary_port"]=>
int(443)
["local_ip"]=>
string(9) "10.9.1.40"
["local_port"]=>
int(27100)
["request_header"]=>
string(135) "POST /token HTTP/1.1
Host:oauth2.googleapis.com
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 325
"
}
string(146) "HTTP/1.1 404 Not Found
Connection: Close
Content-Type: text/plain
Date: Thu, 21 Jul 2022 18:51:08 GMT
Content-Length: 18
404 page not found"
问题
我实在是找不出php curl返回404的原因 ,用postman直接请求 https://oauth2.googleapis.com/token ,返回的就是js请求的结果。
我认为js请求的返回结果是正常的,但是php也是用postman转的代码,我就加了Content-Length和Host。
我对curl了解不是很多实在找不出俩者不一样的原因。
php最后返回的404的结果和用谷歌官网的sdk请求的结果相同……
有说跳转路径非授权路径的,但证个确定是授权路径,所以排除这个原因。
php curl应该怎么改才能返回js一样的回执?
这个Google应用为web应用,是否和如何请求有关?
/(ㄒoㄒ)/~~我怀疑web应用必须用前端js等请求,但是也感觉不太合理……