Debug Ajax Requests with the Browsers Copy as cURL Feature

Debugging Ajax requests can be annoying, especially when you are sending a lot of data over like serializing an entire form. Chrome, Opera, Safari, Firefox, and other browsers all now include the ability to copy an Ajax request as cURL. This is really useful for debugging and testing while you are working locally.

Here is how you can copy the cURL request from two popular browsers:

Once you’ve copied it, your clipboard will contain a string like this with everything that is sent in the request

curl 'http://laravel-5.5.dev/api/test' -H 'Pragma: no-cache' -H 'Origin: http://laravel-5.5.dev' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: XSRF-TOKEN=eyJpdiI6IlZIQWFLVEpmcys1M0pRNDE4N2FrQnc9PSIsInZhbHVlIjoiZ205N3haN0htMHU2b05wcmdQVGk4cmZYS1hqaENvXC9GVGdqWkZGZTRjQ1Z5MVhVU0NBWXhCSng5QWtzNVNlUTIydDJlRGlONmErQzk4VHpDVWc4cWVRPT0iLCJtYWMiOiJmNWY0NTJmYWI1ZDMxNWIzNjY0OTQ2YjlmNzU4NGM3MWY4NWFlNjAzMWU0NWI5Yjk5NmE5ZDUzZWI2YTE4Zjc5In0%3D; laravel_session=eyJpdiI6IlpmSGptSENFSXc5SmEzTmdmQitvc2c9PSIsInZhbHVlIjoiT1dZcld0eldKTXpIMjVUMUJaWHYySWswd2djSGR0SEFmZnhoTDNxbGJYU1BWZU9DTVlaK1A1UFwvaHlSVXdmOTZicmM5cTFnMjd5TEJveUk3UWJSTnZ3PT0iLCJtYWMiOiIzYzQ3ODU4YTZkNDAzYTg5NDQ1MDBmYTE4YmZjNjE5NzNmYTQxZDk0OGVhZDkxNjBkODI4NTRiZWU1ZWZkM2EyIn0%3D' -H 'Connection: keep-alive' -H 'Referer: http://laravel-5.5.dev/' -H 'DNT: 1' --data 'name=John&email=test%40test.com&age=67' --compressed

This allows you to paste it into a terminal and quickly run it again, or paste it into a text editor and make changes on the fly. The only downside is with encrypted strings it gets messy.

So you can copy and paste it into a service like curl-to-php and it’ll convert into PHP compatible curl code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://laravel-5.5.dev/api/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John&email=test%40test.com&age=67");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = "Pragma: no-cache";
$headers[] = "Origin: http://laravel-5.5.dev";
$headers[] = "Accept-Encoding: gzip, deflate";
$headers[] = "Accept-Language: en-US,en;q=0.8";
$headers[] = "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
$headers[] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";
$headers[] = "Accept: */*";
$headers[] = "Cache-Control: no-cache";
$headers[] = "X-Requested-With: XMLHttpRequest";
$headers[] = "Cookie: XSRF-TOKEN=eyJpdiI6IlZIQWFLVEpmcys1M0pRNDE4N2FrQnc9PSIsInZhbHVlIjoiZ205N3haN0htMHU2b05wcmdQVGk4cmZYS1hqaENvXC9GVGdqWkZGZTRjQ1Z5MVhVU0NBWXhCSng5QWtzNVNlUTIydDJlRGlONmErQzk4VHpDVWc4cWVRPT0iLCJtYWMiOiJmNWY0NTJmYWI1ZDMxNWIzNjY0OTQ2YjlmNzU4NGM3MWY4NWFlNjAzMWU0NWI5Yjk5NmE5ZDUzZWI2YTE4Zjc5In0%3D; laravel_session=eyJpdiI6IlpmSGptSENFSXc5SmEzTmdmQitvc2c9PSIsInZhbHVlIjoiT1dZcld0eldKTXpIMjVUMUJaWHYySWswd2djSGR0SEFmZnhoTDNxbGJYU1BWZU9DTVlaK1A1UFwvaHlSVXdmOTZicmM5cTFnMjd5TEJveUk3UWJSTnZ3PT0iLCJtYWMiOiIzYzQ3ODU4YTZkNDAzYTg5NDQ1MDBmYTE4YmZjNjE5NzNmYTQxZDk0OGVhZDkxNjBkODI4NTRiZWU1ZWZkM2EyIn0%3D";
$headers[] = "Connection: keep-alive";
$headers[] = "Referer: http://laravel-5.5.dev/";
$headers[] = "Dnt: 1";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

I’ve found these two tools infinitely useful and I’d love to see a service like that curl-to-php that converts a cURL request into Guzzle compatible code.

PHP Unanimously Approves Short List Syntax for Array Destructuring

The PHP RFC for square bracket syntax for array destructuring assignment has passed and will be included in PHP 7.1 The vote needed a 2/3 majority to be accepted, and it passed with a unanimous vote in favor.

Short array syntax was added to PHP 5.4 and it allowed you to define arrays in a square bracket style:

// Old array style 
$array = array(1, 2, 3); 
// 5.4 square bracket style. 
$array = [1, 2, 3];

With the accepted proposal it creates an alternative to using “list” for destructuring an array. In all previous versions of PHP this works like this:

list($a, $b, $c) = array(1, 2, 3);

Now you can extract using a square bracket just as you do for assignment:

[$a, $b, $c] = [1, 2, 3];
["a" => $a, "b" => $b] = ["a" => a, "b", => 2];

It also works in foreach loops:

foreach ($points as ["x" => $x, "y" => $y]) {
    var_dump($x, $y);
}

You can find out more from the [PHP RFC](https://wiki.php.net/rfc/short_list_syntax_ and look for this in the PHP 7.1 release.