Line

PHP Line Notify(Line 通知)

1.Line Notify後台服務

Line Notify 官網

a.點擊登錄服務

b.輸入資訊

Callback URL 是最重要的 用來返回code資訊以及綁定使用者Line Notify

c.確認資訊

登錄後再去剛剛填寫的email收信認證

d.認證後 會多出Client ID&Client Secret

2.綁定使用者的Line Notify

讓使用者點擊這串網址
https://notify-bot.line.me/oauth/authorize?response_type=code&scope=notify&response_mode=form_post&client_id=9q2mFN9H8rKJJBCNslVzHI&redirect_uri=https://badgameshow.com/laichao/test/push.php&state=123
client_id = Line Notify上的Client ID
redirect_uri = Callback URL

3.Callback後取得code

$_POST[“code”]

4.code取得後去換Token

redirect_uri = Callback URL
client_id = Line Notify上的Client ID
client_secret = Line Notify上的Client Secret
code = $_POST[“code”]
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://notify-bot.line.me/oauth/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'redirect_uri' => 'https://badgameshow.com/laichao/test/push.php',
'client_id' => '9q2mFN9H8rKJJBCNslVzHI',
'client_secret' => 'client_secret',
'code' => $_POST["code"]),
  CURLOPT_HTTPHEADER => array(
    "Cookie: XSRF-TOKEN=d98fa9e6-f9d6-46e2-ac3e-4df0c7b3f834"
  ),
));

$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response);
$access_token = $obj->{'access_token'};

5.利用Token來推播訊息

Header
Authorization: Bearer = $access_token
body
message = 推播訊息
imageFullsize = 圖片地址
imageThumbnail = 圖片地址
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://notify-api.line.me/api/notify",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => array('message' => '尻尻尻尻Test'),
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer " . $access_token
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

5.加入後的訊息

發表迴響