使用 CodeIgniter cURL 通过 UrbanAirship 发送推送通知
Posted
技术标签:
【中文标题】使用 CodeIgniter cURL 通过 UrbanAirship 发送推送通知【英文标题】:Using CodeIgniter cURL to send a push notification via UrbanAirship 【发布时间】:2011-09-18 06:16:58 【问题描述】:我设置了一个 urbanairship 帐户来向我的应用发送推送通知。这一切正常,但现在我正在尝试将它与我的 codeigniter 管理站点集成,以便推送通知由 UA 发送并一步存储到数据库中。我正在尝试使用 cURL 库并遵循 UA API 文档(http://urbanairship.com/docs/push.html),但每次我收到 404 错误。但是,如果我取出 cURL 行,数据就会很好地添加到数据库中,(因此它可以从表单中正确接收数据)。
这是我的控制器中的功能:
函数 saveAnnouncement()
$this->load->helper('html'); $this->load->library('session'); $this->load->helper('json'); $this->load->library('curl'); $new_announcement = $this->input->post('announcement_text'); if($this->session->userdata('logged_in') != true) redirect('/admin/login'); $this->curl->create('https://go.urbanairship.com/api/push/broadcast/'); $this->curl->http_login('<application key>', '<master secret>'); $announcement_push = array('aps'=>array('alert'=>$new_announcement, 'sound'=>'default')); $announcement_push['encoded_data'] = json_encode($announcement_push); $this->curl->post($announcement_push); $this->curl->execute(); $this->load->model('Announcements'); $this->Announcements->Add($new_announcement); redirect('/admin/announcements');
我是 codeigniter、curl 和 urbanairship 的新手,所以你可以想象这是一场噩梦。将不胜感激任何可用的帮助!
谢谢!
【问题讨论】:
我在这里为urbanairship创建了一个codeigniter库:github.com/caioiglesias/Urban-Airship-CodeIgniter-library 现在,他们想要钱!没有更多的免费推送! 【参考方案1】:您是否已将 curl 配置设置为信任您尝试连接的站点的 ssl 证书?先试试这个
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
如果它有效 - 这就是问题所在。然后你应该正确设置你的 curl 连接并添加特定的证书。
【讨论】:
感谢您的建议。不幸的是,它仍然给我 404 错误:S【参考方案2】:php 中的这段代码,Urbanairship
,curl
为我工作:
define ('URBAN_APP_MASTERKEY', XXXXXX);
define ('URBAN_APIKEY',XXXXX);
define ('URBAN_APP_SECRETKEY',XXXXXX);
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$contents = array();
$contents['badge'] = "1";
$contents['alert'] = "Howdy, doody";
$contents['sound'] = "cat.caf";
$devices = array('device_tokens');
$push = array("aps" => $contents);
$push['device_tokens'] = $devices;
$json = json_encode($push);
$url = PUSHURL;
echo $json; //display the actual content
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, URBAN_APIKEY . ':' . URBAN_APP_MASTERKEY);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$output = curl_exec($ch);
if($response['http_code'] != 200)
echo "Got negative response from server, http code: ".
$response['http_code'] . "\n";
else
echo "Wow, it worked!\n";
curl_close($ch);
【讨论】:
这会让您发送到单个设备吗? $devices 是 phoneID 数组吗? @Siriss 是的 $devices 是一组电话 ID。它将发送到各个设备 谢谢 Darshit- $devices 是如何填充的?我被困在这个问题上。 @Siriss 在我的例子中,它是从数据库中填充的......在这里显示一些代码【参考方案3】:如果您不熟悉 Codeigniter,请使用普通 curl!
public function pushNotification()
// Urban AirShip Doc : http://docs.urbanairship.com/connect/android_push.html
// create the contents of the android field
// sample JSON => "audience": "all", "notification": "alert": "Hello!", "device_types": ["android"]
$android = array();
$android['audience'] = "all";
$android['notification'] = array('alert'=>'Hello !');
$android['device_types'] = array("android");
// convert the dictionary to a json string
$data = json_encode($android);
// open connection
$ch = curl_init();
// the url and credentials for posting to urban airship
$url = 'https://go.urbanairship.com/api/push/';
$username = "YourAPIkey";
$password = "YourMasterSecretKey";
// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/vnd.urbanairship+json; version=3;'));
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute post
$result = curl_exec($ch);
$arrayResult=json_decode($result);
// close connection
$res = curl_close($ch);
if($arrayResult->ok == 1)
print "Success";
else
print "Error";
参考:http://blog.jamesbaca.net/?p=385
【讨论】:
以上是关于使用 CodeIgniter cURL 通过 UrbanAirship 发送推送通知的主要内容,如果未能解决你的问题,请参考以下文章
如何删除 codeigniter 代码中的 index.php?
在 Twitter / CodeIgniter 项目中安装 cURL