1

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 !

Kuldeep J
  • 382
  • 5
  • 12
MisterSalva
  • 21
  • 1
  • 8
  • 1
    A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a PHP script. Check your server error logs to find out the exact error message. – aynber Dec 01 '20 at 15:13
  • I have any information on my log... Maybe OVH don't refer it ? Do you have any idea ? – MisterSalva Dec 01 '20 at 15:29
  • Try to use absolute path --> include('../bdd.php'); – Florent Dec 01 '20 at 15:30
  • @Florent, I have try but nop... I have change my .ovhconfig files, for the environment and access to error message, and postman return me that :
    Fatal error: Uncaught Exception: Curl failed: No URL set! in /home/pharmadedx/www/meet/appIos/messageNotification.php:61 Stack trace: #0 {main} thrown in /home/pharmadedx/www/meet/appIos/messageNotification.php on line 61
    – MisterSalva Dec 01 '20 at 15:43
  • In the phpinfo();, I have find information about CURL, and on the row "http2" we have no, and on my localhost we have yes, so I think issue comes from it ? – MisterSalva Dec 01 '20 at 15:54

1 Answers1

0

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

MisterSalva
  • 21
  • 1
  • 8
  • Could you add more details on the solution? – Ingo Steinke May 19 '22 at 08:08
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 19 '22 at 08:09
  • 1
    Just need to enable HTTP/2 on server. – MisterSalva May 20 '22 at 15:26