這是小主本人第一次跟ios打配合,可以說是差點就暴走了,廢話不多說看代碼
//模擬post提交
public static function http_post_json($json,$t_url) {
$postData = http_build_query($json);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $t_url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl);
curl_close($curl);
return $r;
}
這是小主專門給ios官方接口寫的POST的CURL請求,ios也有官方說明必須用application/x-www-form-urlencoded請求頭的方式來請求這個接口
因爲小主比較懶 client_secret 這個算法實在是不想算,於是就交給了ios客服端大哥傳給我,這個也只是傳一次,我這邊接口會寫死,因爲過期時間有6個月,但是客戶端改會比較麻煩,就決定6個月ios生成一次我這邊更新一下
下面是小主請求post方法的代碼也是爲了大家方便
public function verifyReceipt(){
$t_url = 'https://appleid.apple.com/auth/token';
$json = [
'client_id' => 應用程序標識符,
'refresh_token' => 客戶端返回的token,
'client_secret' => 客戶端生成的密鑰,
'code' => 從應用程序的用戶代理收到的授權代碼,
'grant_type' => 'authorization_code',(這個呢,是小主理解複雜了,其實就是要傳authorization_code,才鬧出這麼大的笑話來,大家不必擔心,小主已經給大家踩過坑了)
];
$info = $this->http_post_json($json,$t_url);
}
這些就可以完美解決返回invalid_client問題