LoginSignup
0

More than 5 years have passed since last update.

AWS SDK for PHP v3でEndpointArnを取得するまでの道のり

Posted at

AWS SNSを使ってMobile Pushするときに必要な技です。

  • createPlatformEndpointは登録済みのTokenを渡すと発行済みのendpointArnを返します。
  • しかし、endpointArnに対してCustomUserDataがセットされてる場合、TokenCustomUserDataが完全一致しない限りcreatePlatformEndpointが失敗(Aws\Sns\Exception\SnsException)します。
  • endpointArnに紐づくCustomUserDataを知るにはgetEndpointAttributesを使いますがendpointArnが必要です。
  • TokenからendpointArnを知る方法はcreatePlatformEndpointしかありません。

そう、缶切りは缶詰の中にはいっているのです(´・ω・`)

公式ドキュメントによるとExceptionをキャプチャして解析する必要があるみたいなのでEndPointArnを取得するメモを残します。

try {
    $platform = $client->createPlatformEndpoint([
        'PlatformApplicationArn'    => '*****',
        'Token'                     => '*****',
    ]);
    $endpointArn = $platform['EndpointArn'];
}catch(Aws\Sns\Exception\SnsException $e){
    $message = $e->getMessage();
    if( !preg_match('#^.+(<ErrorResponse\s.+?</ErrorResponse>)#s',$message,$match) ) throw $e;
    $xml = new SimpleXMLElement($match[1]);
    $message = $xml->Error->Message;
    if( !preg_match('/Endpoint (arn:aws:sns[^ ]+)/',$message,$match) ) throw new Exception($message);
    $endpointArn = $match[1];
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0