Posted By

hisamu on 10/05/10


Tagged

ajax php jquery proxy


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

bobbym245
mmcachran
wirenaught


Jquery AJAX cross-domain proxy with php


 / Published in: PHP
 

URL: http://www.lsouza.pro.br

A PHP proxy for jQuery AJAX cross-domain requisitions, created to support friendly-url API\\\\'s.

  1. <?php
  2. $base_url = 'http://www.yoursite.com/api/';
  3. $chamada = explode('/', $_SERVER['PHP_SELF']);
  4. $chamada = $chamada[2]; // if this php file is in a subdirectory you may change this key
  5. $url = $base_url.$chamada;
  6. $method = (!empty($_POST)) ? 'post' : 'get';
  7.  
  8. $ch = curl_init();
  9.  
  10. if ($method == 'get')
  11. {
  12. if ($_SERVER['QUERY_STRING']) $url .= '?' . $_SERVER['QUERY_STRING'];
  13.  
  14. curl_setopt($ch, CURLOPT_HEADER, 0);
  15. curl_setopt($ch, CURLOPT_URL, $url);
  16. }
  17. else
  18. {
  19. curl_setopt($ch, CURLOPT_POST, true);
  20. curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
  21. curl_setopt($ch, CURLOPT_URL, $url);
  22. }
  23.  
  24. $data = curl_exec($ch);
  25. curl_close($ch);
  26. ?>
  27.  
  28. <!-- Example -->
  29. $.get('proxy.php/my_api_call');
  30. $.post('proxy.php/my_post_api', {id:user_id});

Report this snippet  

You need to login to post a comment.