Skip to content

Instantly share code, notes, and snippets.

@zackchandler
Created June 3, 2009 05:47
Show Gist options
  • Select an option

  • Save zackchandler/122805 to your computer and use it in GitHub Desktop.

Select an option

Save zackchandler/122805 to your computer and use it in GitHub Desktop.
var proxy = 'http://localhost/foobar/proxy.php';
$('form#new_customer').submit(function(){
data = '<customer>';
data += '<name>' + $('#customer_name').val() +'</name>';
data += '</customer>';
$.ajax({
url: proxy + '?api_path=customers.xml',
contentType: 'application/xml',
type: 'POST',
data: data,
dataType:'xml',
beforeSend: function(){
if ($.trim($('#customer_name').val()) == '')
return false
},
success: function(data){
alert('Created customer with customer number: ' + $(data).find('customer-number').text());
}
})
return false;
})
<?php
// Loosely based on the Jason Levitt PHP Proxy example for Yahoo! Web services.
define ('HOSTNAME', 'http://foobar.localhost.com:3000/');
$path = ($_POST['api_path']) ? $_POST['api_path'] : $_GET['api_path'];
// Build url and add any parameters
$url = HOSTNAME.$path.'?';
while ($element = current($_GET)) {
$url .= key($_GET).'='.$element.'&';
next($_GET);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'abc123:X');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $HTTP_RAW_POST_DATA);
}
$xml = @curl_exec($ch);
curl_close($ch);
header("Content-Type: text/xml");
echo $xml;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment