500 internal server error - Try to push APN on my OVH server

472 views Asked by At

So to explain my issues briefly, I develop an iOS application. I want to built push notification. I have built a PHP script to that, this works fine when I do test on Postman and on localhost.

When I put this script on my server (OVH), I change the URL on my Postman app, and relaunch it, but this return a 500 internal server error. I have try to change the url of API ("api.development.push.apple.com" to "api.push.apple.com") but doesn't work.

I also tried, directly from my app, with Alamofire request but same issue.

I put my PHP script below, and a screenshot of my Postman request. (with specific information hidden)

<?php

include('../bdd.php');

  $idNotif = $_POST['notifId'];
    $req = $bdd -> prepare('SELECT tokenDevice FROM users WHERE id=:idUser');
    $req -> execute(array(
        'idUser' => $idNotif
    ));
    $data = $req -> fetch();
    $tokenDevice = $data['tokenDevice'];
      $keyfile = 'AuthKey.p8';               // Your p8 Key file
      $keyid = 'kid';                            // Your Key ID
      $teamid = 'tid';                           // Your Team ID (see Developer Portal)
      $bundleid = 'bid';                // Your Bundle ID
      $url = 'https://api.development.push.apple.com';  // development url, or use http://api.push.apple.com for production environment
      $token = $tokenDevice;                            // Device Token

      $message = '{"aps":{"alert":"Vous avez un nouveau message !","sound":"default"}}';

      $key = openssl_pkey_get_private('file://'.$keyfile);

      $header = ['alg'=>'ES256','kid'=>$keyid];
      $claims = ['iss'=>$teamid,'iat'=>time()];

      $header_encoded = base64($header);
      $claims_encoded = base64($claims);

      $signature = '';
      openssl_sign($header_encoded . '.' . $claims_encoded, $signature, $key, 'sha256');
      $jwt = $header_encoded . '.' . $claims_encoded . '.' . base64_encode($signature);

      // only needed for PHP prior to 5.5.24
      if (!defined('CURL_HTTP_VERSION_2_0')) {
          define('CURL_HTTP_VERSION_2_0', 3);
      }

      $http2ch = curl_init();
      curl_setopt_array($http2ch, array(
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
        CURLOPT_URL => "$url/3/device/$token",
        CURLOPT_PORT => 443,
        CURLOPT_HTTPHEADER => array(
          "apns-topic: {$bundleid}",
          "authorization: bearer $jwt"
        ),
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => $message,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HEADER => 1
      ));

      $result = curl_exec($http2ch);
      if ($result === FALSE) {
        throw new Exception("Curl failed: ".curl_error($http2ch));
      }

      $status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);
      // echo $status;

      function base64($data) {
        return rtrim(strtr(base64_encode(json_encode($data)), '+/', '-_'), '=');
      }

    // echo json_encode($response2);
?>

Screenshot of postman post on localhost (works) :
postman screenshot 200 works fine
postman screenshot 200 works fine

Screenshot of postman post on my OVH server (doesn't work) :
postman screenshot 500 error
postman screenshot 500 error

If you can help me to understand what is wrong I sincerely appreciate it !

Thank you !

1

There are 1 answers

3
MisterSalva On BEST ANSWER

Old question, but, patched with environment settings. It's mainly about 'http2' configuration.