如何将此代码转换为PHP

时间:2011-11-25 12:21:51

标签: php apple-push-notifications

我需要知道如何将下面的代码转换为.php代码(我对它不好)

代码:

curl -X POST -u "<application key>:<master secret>" \
-H "Content-Type: application/json" \
--data '{"device_tokens": ["<token>"], "aps": {"alert": "Hello!"}}' \
https://go.xxx.com/api/push/
编辑:我试过这个:

<? 
$msg = "hello from bob";

$token = "72F75474E3360C4C2F26C6AB16FC1E638FE55FCAC92EE4EB4C196123XXXXXXXX";

exec('curl -X POST -u "_rEUqtOtSmSVEBd8uMfdtg:vpB2wmR8Q_2HZ_Jmi37t-Q" \
    -H "Content-Type: application/json" \
    --data \'{"aps": {"alert": "'.$msg.'", "sound": "default"}, "device_tokens": ["'.$token.'"]}\' \
    https://go.xxx.com/api/push/
');
?>

2 个答案:

答案 0 :(得分:0)

通过cURL library。您应该能够通过PHP手册中不同curl_*函数的文档找到有关此特定问题的足够信息。

答案 1 :(得分:0)

您可以尝试使用PHP's cURL functions来完成此操作。以下应该达到你想要的(未经测试):

$handle = curl_init("https://go.xxx.com/api/push/");
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($handle, CURLOPT_USERPWD, "<application key>:<master secret>");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, '{"device_tokens": ["<token>"], "aps": {"alert": "Hello!"}}')

$result = curl_exec($handle);
curl_close($handle);