php 返回数据 php如何进行多次转跳然后获取返回头信息?
最近使用火车头进行采集信息,在采集内容时。里面有个下载地址是利用js传参POST到页面获得实际转跳地址。就是采集A获得B,在利用B转跳获取实际网盘地址,请问怎么写这个PHP的插件获取最终的网盘地址。
顺晟科技
2022-09-15 16:10:36
218
https://github.com/unkaer/xia...
这是一段小米运动账号的登录请求
我想把此文件里面的 request_post 方法改为 guzzlehttp/guzzle
方式来写
原本的 request_post 方法
function request_post($url = '', $post_data = array(), $header = array()) {
if (empty($url) || empty($post_data)) {
return false;
}
if (empty($header)) {
$header = 0;
}
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, true);// 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
// print_r($data."</br>");
return $data;
}
// 调用方法
$user = '';
$password = '';
$url = 'https://api-user.huami.com/registrations/+86'. $user.'/tokens';
$data = [
'client_id' => 'HuaMi',
'password' => $password,
'redirect_uri' => 'https://s3-us-west-2.amazonaws.com/hm-registration/successsignin.html',
'token' => 'access',
];
$header = [
'Content-Type:application/x-www-form-urlencoded;charset=UTF-8',
'User-Agent:MiFit/4.6.0 (iPhone; iOS 14.0.1; Scale/2.00)',
];
request_post($url, $data, $header)
我改成 guzzlehttp/guzzle
,使用的 laravel
use Illuminate\Support\Facades\Http;
$user = '';
$password = '';
$url = 'https://api-user.huami.com/registrations/+86'. $user.'/tokens';
$data = [
'client_id' => 'HuaMi',
'password' => $password,
'redirect_uri' => 'https://s3-us-west-2.amazonaws.com/hm-registration/successsignin.html',
'token' => 'access',
];
$header = [
'User-Agent' => 'MiFit/4.6.0 (iPhone; iOS 14.0.1; Scale/2.00)',
];
//改为的 guzzlehttp/guzzle 方式
$response = Http::asForm()->withHeaders($header)->post($url, $data);
$user
传小米运动的账号,$password
传小米运动的密码
这两个请求正确的情况下,原本的 request_post
方法是返回http状态码303,我改的guzzlehttp/guzzle 方式返回是200,数据也不一样
已解决。添加allow_redirects
为false
即可
namespace Illuminate\Http\Client
class PendingRequest
//添加此方法
withoutRedirecting()
public function login(string $user, string $password)
{
$url = 'https://api-user.huami.com/registrations/+86'. $user.'/tokens';
$data = [
'client_id' => 'HuaMi',
'password' => $password,
'redirect_uri' => 'https://s3-us-west-2.amazonaws.com/hm-registration/successsignin.html',
'token' => 'access',
];
$header = [
'User-Agent' => 'Dalvik/2.1.0 (Linux; U; Android 11; M2012K11AC Build/RKQ1.200826.002)',
];
$location = Http::asForm()->withHeaders($header)->withoutRedirecting()->post($url, $data)->header('Location');
parse_str(parse_url($location, PHP_URL_QUERY), $query);
if(!isset($query['access'])){
throw new \Exception('登录失败');
}
$url = "https://account.huami.com/v2/client/login";
$data = [
'app_name' => 'com.xiaomi.hm.health',
'app_version' => '4.6.0',
'code' => $query['access'],
'country_code' => 'CN',
'device_id' => '2C8B4939-0CCD-4E94-8CBA-CB8EA6E613A1',
'device_model' => 'phone',
'grant_type' => 'access_token',
'third_name' => 'huami_phone',
];
$response_query = Http::asForm()->withHeaders($header)->withoutRedirecting()->post($url, $data)->json();
return [$response_query['token_info']['login_token'], $response_query['token_info']['user_id']];
}
17
2022-11
09
2022-11
19
2022-10
19
2022-09
15
2022-09
15
2022-09