Dienstag, 5. November 2013

PHP-Proxy for Carte

Hi again!
my first post was dealing with a JSP-based proxypage to enable "crosssite javascripting" access to the Carte-Service.
Now it´s time to serve the same approach for the PHP-folks out there.
Basically, all you need is:
  • PHP (I used version 5.5.4, but any v5 should be good I think)
  • curl-Extension activated in your PHP (please refer to php.net for details)
As the details already had been discussed in the first post, I will now just paste the code for the proxy.php file which will do the same "proxy magic":
<?
$url = $_GET["url"];
$url .= "?1=1";
foreach ( $_GET as $getKey => $getVar ) {
  //print $getVar;
  if ( $getKey != "url" ) {
    $url .= "&".$getKey."=".str_replace(" ","+",urlencode($getVar));
  }
}
// tested with such urls:
// http://localhost/test/proxy.php?url=http://localhost:8888/kettle/transStatus/&name=Row+generator+test&id=ee8d2d9d-da0a-404c-8488-ededf6b176df&xml=y
//echo $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERPWD, "cluster:cluster");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);
$content = curl_getinfo($curl,CURLINFO_CONTENT_TYPE);
header("Content-type: ".$content);
// pretty ugly but I could not come up with a simpler approach to separate the HTTP-Headers from the Content
$response_arr = explode("\r\n",$response);
$startresponse = false;
$responsetext = "";
foreach (  $response_arr as $line ) {
  if ( trim($line) == "" ) {
    $startresponse = true;
  }
  if ( $startresponse ) {
    $responsetext .= $line;
  }
}
echo $responsetext;
?>


To download the file directly, head over to github: proxy.php

Please mind the linebreaks when downloading/installing the file.
Anyway, I hope this file will satisfy the PHP-guys and be of some use for you out there!
Thanks for reading, see you next time.

Keine Kommentare:

Kommentar veröffentlichen