首页 > 解决方案 > 收到无效的 JSON 有效负载。未知名称“”:根元素必须是消息

问题描述

我正在使用“使用 REST API 创建动态链接”并为此使用了以下代码。这是使用 google firebase 动态链接 API。

$long_url = "https://etisalatone.page.link/?link=https://www.example.com/reload/10&apn=com.omobio.etisalatone";

$api_url = 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=';

$data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds

if ($type == 'POST') {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}

$api_time_track = microtime();

$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

结果产生以下错误:

收到无效的 JSON 有效负载。未知名称“”:根元素必须是消息。

标签: phpapifirebase-dynamic-links

解决方案


推荐阅读