AndroidCustomizedcast.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. require_once(dirname(__FILE__) . '/../AndroidNotification.php');
  3. class AndroidCustomizedcast extends AndroidNotification {
  4. function __construct() {
  5. parent::__construct();
  6. $this->data["type"] = "customizedcast";
  7. $this->data["alias_type"] = NULL;
  8. }
  9. function isComplete() {
  10. parent::isComplete();
  11. if (!array_key_exists("alias", $this->data) && !array_key_exists("file_id", $this->data))
  12. throw new Exception("You need to set alias or upload file for customizedcast!");
  13. }
  14. // Upload file with device_tokens or alias to Umeng
  15. function uploadContents($content) {
  16. if ($this->data["appkey"] == NULL)
  17. throw new Exception("appkey should not be NULL!");
  18. if ($this->data["timestamp"] == NULL)
  19. throw new Exception("timestamp should not be NULL!");
  20. if ($this->data["validation_token"] == NULL)
  21. throw new Exception("validation_token should not be NULL!");
  22. if (!is_string($content))
  23. throw new Exception("content should be a string!");
  24. $post = array("appkey" => $this->data["appkey"],
  25. "timestamp" => $this->data["timestamp"],
  26. "validation_token" => $this->data["validation_token"],
  27. "content" => $content
  28. );
  29. $ch = curl_init($this->host . $this->uploadPath);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  32. curl_setopt($ch, CURLOPT_POST, 1);
  33. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
  34. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
  36. $result = curl_exec($ch);
  37. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  38. $curlErrNo = curl_errno($ch);
  39. $curlErr = curl_error($ch);
  40. curl_close($ch);
  41. //print($result."\r\n");
  42. if ($httpCode == "0") //time out
  43. throw new Exception("Curl error number:" . $curlErrNo . " , Curl error details:" . $curlErr . "\r\n");
  44. else if ($httpCode != "200") //we did send the notifition out and got a non-200 response
  45. throw new Exception("http code:" . $httpCode . "\r\n" . "details:" . $result . "\r\n");
  46. $returnData = json_decode($result);
  47. if ($returnData["ret"] == "FAIL")
  48. throw new Exception("Failed to upload file, details:" . $result . "\r\n");
  49. else
  50. $this->data["file_id"] = $returnData["data"]["file_id"];
  51. }
  52. function getFileId() {
  53. if (array_key_exists("file_id", $this->data))
  54. return $this->data["file_id"];
  55. return NULL;
  56. }
  57. }