bitlyにアカウント登録します。
Freeだと1000件/month なので、1日30件が限度。
$long_url = 'https://stackoverflow.com/questions/ask'; $apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks'; $genericAccessToken = '***'; $data = array( 'long_url' => $long_url ); $payload = json_encode($data); $header = array( 'Authorization: Bearer ' . $genericAccessToken, 'Content-Type: application/json', 'Content-Length: ' . strlen($payload) ); $ch = curl_init($apiv4); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $result = curl_exec($ch); $resultToJson = json_decode($result); if (isset($resultToJson->link)) { echo $resultToJson->link; } else { echo 'Not found'; }
$ php bitly.php
https://bit.ly/3tp7C1c
OK、全部繋げてみよう
なるほどー