当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to send the JSON also from the PHP API

问题描述:

I'm sending a text push from the php API code successfully, but I wonder how to send the JSON also from the PHP API

My problem is that when I send JSON from the PHP API code it received like like a "message" type.

When i send the same JSON code from the parse control panel it works fine

I so some hint to send "uri" key in the data, but it didn't help.

What am I missing?

Thanks.

this is the PHP code that i'm using

<?php

$APPLICATION_ID = "gggggggggggggggg";

$REST_API_KEY = "xxxxxxxxxxxxxxxxxxx";

//$MESSAGE = "your-alert-message";

if (!empty($_POST)) {

$errors = array();

foreach (array('app' => 'APPLICATION_ID', 'api' => 'REST_API_KEY', 'body' => 'MESSAGE') as $key => $var) {

if (empty($_POST[$key])) {

$errors[$var] = true;

} else {

$$var = $_POST[$key];

}

}

if (!$errors) {

$url = 'https://api.parse.com/1/push';

if(strstr($MESSAGE,"{")){ //json

$data['data_type']='json';

//$MESSAGE = json_decode($MESSAGE);

}

//, 'uri' => $MESSAGE

$data = array(

'channel' => 'test1',

'type' => 'android',

'expiry' => 1451606400,

'data_type' => 'json',

'data' =>array(

'alert'=> "the link2",

'uri' => $MESSAGE

),

'uri' => $MESSAGE

);

//var_dump( $data );die;

//if(strstr($MESSAGE,"{")) //json

//$data['data_type']='json';

$_data = json_encode($data);

//var_dump( $_data );die;

$headers = array(

'X-Parse-Application-Id: ' . $APPLICATION_ID,

'X-Parse-REST-API-Key: ' . $REST_API_KEY,

'Content-Type: application/json',

'Content-Length: ' . strlen($_data)

);

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);

}

}

?>


In the parse.com website there is a tool that you can send PUSH and in this tool you can send push with 2 types: plain text, or JSON.

it's look like that

When I try to send push with this parse.com tool in both types it works fine and I get the push properly

But, when I try to send to push from my PHP API code as a "JSON" I always got it as a "plain text"

What am I'm doing wrong ?

网友答案:

It depends on your JSON structure for example i want to send this:

{
 "data": {
    "date": "2015-12-09",
    "message": "and when you open this we will show you a message.",
    "title": "6 Notice Click On It :)))",
    "url": ""
   },
"is_background": false
}

and in your php you should send like this:

$data = array(
     'where' => '{}',  // send to all users
    // 'channels' => '{my_channel_name}',
    'data' => array(
        'is_background'=>false,
        'data'=>array(
            'date'=>'2015-12-09',
            'message'=>'and when you open this we will show you a message',
            'title'=>' Notice Click On It',
            'url'=>''
        ),
    ),
);
分享给朋友:
您可能感兴趣的文章:
随机阅读: