Laravel HTTP 客户端 post 请求不工作
Laravel HTTP Client post request not working
尝试从 Instagram 获取访问令牌
$params = array(
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
);
$ch = curl_init();
$endpoint = $this->getBaseUrl() . 'oauth/access_token';
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
curl_close($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
dd($response,$header_size,$header,$body);
$response = json_decode($response, true);
return $response;
此代码与 CURL 一起工作正常。
Request Data Laravel 文档。
$response = Http::post($this->getBaseUrl() . 'oauth/access_token', [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
]
);
return $response->body();
但无法使用 Laravel Http Client.it return 缺少客户端 ID 但我正在作为参数发送。
我猜你的内容类型是application/x-www-form-urlencoded
,尝试使用asForm()
,
$response = Http::asForm()->post($this->getBaseUrl() . 'oauth/access_token', [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
]
);
return $response->json();
尝试从 Instagram 获取访问令牌
$params = array(
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
);
$ch = curl_init();
$endpoint = $this->getBaseUrl() . 'oauth/access_token';
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
curl_close($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
dd($response,$header_size,$header,$body);
$response = json_decode($response, true);
return $response;
此代码与 CURL 一起工作正常。
Request Data Laravel 文档。
$response = Http::post($this->getBaseUrl() . 'oauth/access_token', [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
]
);
return $response->body();
但无法使用 Laravel Http Client.it return 缺少客户端 ID 但我正在作为参数发送。
我猜你的内容类型是application/x-www-form-urlencoded
,尝试使用asForm()
,
$response = Http::asForm()->post($this->getBaseUrl() . 'oauth/access_token', [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
]
);
return $response->json();