正在解析一个信号 API 响应

Parsing onesignal API response

我想使用 PHP 解析来自 onesignal API 的响应。以下是 API 响应的结构。我想打印唯一的某些参数。我该怎么做?

{
    "adm_big_picture": "",
    "adm_group": "",
    "adm_group_message": {
        "en": ""
    },
    "adm_large_icon": "",
    "adm_small_icon": "",
    "adm_sound": "",
    "spoken_text": {},
    "alexa_ssml": null,
    "alexa_display_title": null,
    "amazon_background_data": false,
    "android_accent_color": "FF9900FF",
    "android_group": "group key",
    "android_group_message": {
        "en": "group message for Android 6.0 and older"
    },
    "android_led_color": null,
    "android_sound": null,
    "android_visibility": 1,
    "app_id": "3beb3078-e0f1-4629-af17-fde833b9f716",
    "big_picture": "https://img.onesignal.com/n/37326fcc-2baa-45da-891c-ca9454a64957.png",
    "buttons": null,
    "canceled": false,
    "chrome_big_picture": "",
    "chrome_icon": "",
    "chrome_web_icon": "https://picture-resource.com/icon-image.png",
    "chrome_web_image": "https://picture-resource.com/image.png",
    "chrome_web_badge": "https://picture-resource.com/badge.png",
    "content_available": false,
    "contents": {
        "en": "English Message"
    },
    "converted": 1,
    "data": {
        "additional": "data",
        "campaign": "25"
    },
    "delayed_option": "immediate",
    "delivery_time_of_day": "2:32PM",
    "errored": 4,
    "excluded_segments": [],
    "failed": 0,
    "firefox_icon": "https://picture-resource.com/firefox_icon.png",
    "global_image": "https://link-to-your-image.com/image-resource-file.png",
    "headings": {
        "en": "English Message Title "
    },
    "id": "fd1723c6-bfaf-4f53-b4f4-0408ff43e18a",
    "include_player_ids": null,
    "include_external_user_ids": null,
    "included_segments": [
        "Subscribed Users"
    ],
    "thread_id": null,
    "ios_badgeCount": 1,
    "ios_badgeType": "Increase",
    "ios_category": "",
    "ios_sound": "explosion.wav",
    "apns_alert": {},
    "isAdm": false,
    "isAndroid": true,
    "isChrome": false,
    "isChromeWeb": true,
    "isAlexa": false,
    "isFirefox": true,
    "isIos": true,
    "isSafari": true,
    "isWP": false,
    "isWP_WNS": false,
    "isEdge": true,
    "large_icon": "https://img.onesignal.com/n/b5cadcf0-1297-4489-b865-545b421d8c5d.png",
    "priority": 5,
    "queued_at": 1580510246,
    "remaining": 0,
    "send_after": 1580510246,
    "completed_at": 1580510247,
    "small_icon": "small_icon",
    "successful": 232,
    "received": 5,
    "tags": null,
    "filters": null,
    "template_id": null,
    "ttl": 777,
    "url": "https://onesignal.com/blog",
    "web_url": null,
    "app_url": null,
    "web_buttons": null,
    "web_push_topic": null,
    "wp_sound": "",
    "wp_wns_sound": "",
    "platform_delivery_stats": {
        "edge_web_push": {
            "successful": 2,
            "failed": 0,
            "errored": 0
        },
        "chrome_web_push": {
            "successful": 26,
            "failed": 0,
            "errored": 0
        },
        "firefox_web_push": {
            "successful": 1,
            "failed": 0,
            "errored": 0
        },
        "android": {
            "successful": 198,
            "errored": 1,
            "failed": 0
        },
        "safari_web_push": {
            "successful": 3,
            "failed": 0,
            "errored": 0
        },
        "ios": {
            "successful": 2,
            "errored": 3,
            "failed": 0
        }
    },
    "ios_attachments": {
        "id": "https://img.onesignal.com/n/23f60ba6-8f2e-4ce1-bdf5-86e6286c2e39.png"
    }
}

我使用了他们给的 PHP 代码,但运气不好

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications/c3971bca-b49f- 47c8-b9a0-ace613a06431?app_id=MYAPPID");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
                           'Authorization: Basic AUTHKEY'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $response = curl_exec($ch);
    curl_close($ch);
        $return["allresponses"] = $response;
    $return = json_encode( $return);

      print("\n\nJSON received:\n");


      $yummy = json_decode($return, true);
      print_r($yummy);
      echo("<br>");
      echo "checking---".$yummy[0]['app_id']; // I donnot know to to access this
      print("\n");

我只想打印某些参数,以便我可以做进一步的处理。但是我无法解析响应。

您的 JSON 是一个对象,而不是一个数组,因此(一旦您解码了它)您可以直接访问它的属性。只需放弃 [0] 就没有问题了。

echo "checking---".$yummy['app_id'];

演示:https://3v4l.org/QKUQN

如果 JSON 是一个数组并且您的数据位于该数组的第一个元素内,那么 [0] 就有意义。但是对于一个简单的对象,没有额外的层可以遍历。