2011年3月26日 星期六

利用curl傳送post by PHP

<?php
define("SERVER_URL", "https://go.urbanairship.com");
define("PUSH", "/api/push/");
define("BROADCAST", "/api/push/broadcast/");
define("APP_KEY", "8Du08OUPTeecwya111");
define("APP_MASTER_SECRET", "EBZ5mQLQR333");

function send($post_data, $url)
{
$post_data = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, APP_KEY.":".APP_MASTER_SECRET);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
        "Content-type: application/json"
));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}

function sendBroadcast($msg, $badge)
{
$url = SERVER_URL.BROADCAST;
$post_data = array (
        "aps" => array( "alert" => $msg,
"badge" => $badge,
"sound" => "frog.caf")
);
send($post_data, $url);
}

function sendPush($msg, $tokens)
{
$url = SERVER_URL.PUSH;
print_r( $tokens);
$post_data = array (
        "aps" => array( "alert" => $msg ),
"device_tokens" => $tokens
);
send($post_data, $url);
}

sendBroadcast("hi", 3);
sendPush("hi", array("BDD076692A89126ED3C5EA057F7C2535312"));

?>

沒有留言:

張貼留言