Notifo Subscribe Request Using PHP, CURL and Jquery


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. ?>

URL: http://www.notifo.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.