在 curl 中使用变量

Using variable inside curl

我正在为多个移动应用程序使用网络 api。我用 PHP 作为我的 api。每个移动应用程序使用不同的 onesignal 帐户。在 React Native 方面,我正在使用 axios 发送 rest API 键。在 api 方面,我想在 curl 中尝试这个 api 键。

我在 curl 中使用传统的点连接。但是当移动应用程序使用 api 时,它 returns 空错误。

$api_key = 'ASDFGHJKLSI';

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                                   'Authorization: Basic ."$api_key".'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

在 curl 中使用变量的方法正确吗?

你的引号有问题。使用

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic ' . $api_key));