Skip to content

Instantly share code, notes, and snippets.

@marzocchi
Created January 2, 2015 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marzocchi/2b7b89ba7866212b4cea to your computer and use it in GitHub Desktop.
Save marzocchi/2b7b89ba7866212b4cea to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\ProcessBuilder;
/**
* @author marzocchi
*/
class RequestToCurlTranslator {
private $curl = 'curl';
public function translate(Request $request) {
$args = [$this->curl];
if ($request->getMethod() !== 'GET') {
$args[] = '-X' . $request->getMethod();
}
$args[] = $request->getUri();
if ($request->getMethod() === 'POST') {
xdebug_break();
}
if ($this->hasContent($request)) {
switch ($request->headers->get('Content-Type')) {
case 'multipart/form-data':
break;
case 'application/x-www-form-urlencoded':
foreach($request->request->keys() as $key) {
$args[] = '-d' . $key . '=' . $request->request->get($key);
}
break;
default:
$args[] = '-d' . $request->getContent();
break;
}
}
$builder = new ProcessBuilder($args);
$commandLine = $builder->getProcess()->getCommandLine();
return $commandLine;
}
private function hasContent(Request $request) {
return in_array($request->getMethod(), ['PUT', 'POST']) || trim($request->getContent()) !== '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment