Notifo使用PHPCURL和Jquery订阅请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Notifo使用PHPCURL和Jquery订阅请求相关的知识,希望对你有一定的参考价值。

Example of how to send a subscribe_user request using Notifo's api. Notifo lets you setup push notifications to mobiles - for any site.

Put the php into a separate file named notifo-proxy.php
  1. // html + jquery
  2. <script type="text/javascript" src="_assets/behaviour/jquery.min.js"></script>
  3. <script type="text/javascript">
  4.  
  5. $(document).ready(function() {
  6. $("#notifo").submit(function(e) {
  7.  
  8. e.preventDefault();
  9.  
  10. $.getJSON("notifo-proxy.php?username=" + $("#username").attr("value"),
  11. function (data) {
  12. switch (parseInt(data.response_code)) {
  13.  
  14. case 2201:
  15. //success
  16. $('.result').html( "Success! " + data.response_message );
  17. break;
  18. case 2202:
  19. //success - already registered
  20. $('.result').html( "You're already subscribed! " + data.response_message );
  21. break;
  22. default:
  23. //error - see the response codes of notifo's api
  24. $('.result').html( "There was an error. " + data.response_message );
  25. }
  26. }
  27. )
  28.  
  29. })
  30. });
  31.  
  32. </script>
  33. <div class="notfio">
  34. <h1>Notifo Example</h1>
  35. <form id="notifo" action="notifo-proxy.php" >
  36. <input type="text" id="username" name="username" value="" />
  37. <input type="submit" value="go" />
  38. </form>
  39. </div>
  40. <div class="result">
  41. <!-- server responses here -->
  42. </div>
  43.  
  44. <?php
  45. //-----------------------------------------------------------------------------------
  46.  
  47. //get username
  48. $username = $_GET['username'];
  49.  
  50. //Create the connection handle
  51. $curl_conn = curl_init();
  52.  
  53. //Set cURL options
  54. curl_setopt($curl_conn, CURLOPT_URL, "https://api.notifo.com/v1/subscribe_user");
  55. curl_setopt($curl_conn, CURLOPT_POST, true);
  56. curl_setopt($curl_conn, CURLOPT_POSTFIELDS,"username=" . $username);
  57. curl_setopt($curl_conn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Use basic authentication
  58. curl_setopt($curl_conn, CURLOPT_USERPWD, "your-servicename:your-servicename-secret");
  59. curl_setopt($curl_conn, CURLOPT_SSL_VERIFYPEER, false); //Do not check SSL certificate
  60. curl_setopt($curl_conn, CURLOPT_RETURNTRANSFER, 1); //Return the result as string
  61.  
  62. // Result from querying URL. Will parse as xml
  63. $output = curl_exec($curl_conn);
  64.  
  65. // close cURL resource.
  66. curl_close($curl_conn);
  67. echo $output;
  68.  
  69. //-----------------------------------------------------------------------------------
  70. ?>

以上是关于Notifo使用PHPCURL和Jquery订阅请求的主要内容,如果未能解决你的问题,请参考以下文章

为啥要使用发布/订阅模式(在 JS/jQuery 中)?

Mailchimp 使用 jQuery AJAX 订阅?

javascript 使用jQuery的简单发布/订阅

如何在 JQUERY 中订阅 MVC 复选框更改事件

Mailchimp API V3 jQuery Ajax POST 订阅者

Mailchimp API V3 jQuery Ajax POST 订阅者