Home » Php » expo – PHP curl behaviour is not consistent

expo – PHP curl behaviour is not consistent

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m developing a backend system for a mobile app using PHP.
The mobile app is developed using Expo.
What i’m trying to do is to send push notification to device through expo server.

I created a helper function to perform a POST request to expo push notification endpoint described here.

/**
 * Perform POST request with curl.
 * 
 * @param string $url
 * @param mixed $params
 * @param array $headers
 * 
 * @return array
 */
function curl_post($url, $params = null, $headers = [])
{
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTPHEADER => $headers,
    ]);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);

    if (!empty($error)) {
        throw new \Error($error);
    }

    return json_decode($response, true);
}

At first, it worked like a charm, but sometimes later, it doesn’t work because Expo server doesn’t recognize the parameters i sent.

The strange thing is, when i used guzzle to perform the request (with the same input), it always worked.
Am i missing some configuration when using curl ?
Or is this a known bug of curl ?

I’m using Windows 10 & XAMPP with PHP 7.3.11 for development, also i’m using Laravel 6 framework.

How to&Answers: