Skip to content

Instantly share code, notes, and snippets.

@sfgarza
Created August 16, 2016 21:57
Show Gist options
  • Save sfgarza/8e57cc1eceabcbcb93738741978ea8d8 to your computer and use it in GitHub Desktop.
Save sfgarza/8e57cc1eceabcbcb93738741978ea8d8 to your computer and use it in GitHub Desktop.
<?php
### PHP command line wrapper for WP Engine's web based WP-CLI
### Example command line: php wp_engine_cli.php -i wp_install_name -c "cli info"
$all_installs = array(
'install1',
'install2',
'install3,
);
# Grab options from command line
$options = getopt("i:c:");
# Break out into variables
$install = $options["i"];
$command = $options["c"];
if ( isset( $command ) ){
if( isset( $install )){
send_request( $install, $command );
}
else{
foreach ( $all_installs as $site ) {
send_request( $site, $command );
}
}
echo "\n";
}
else{
echo "Error - No command inputted\n";
}
function send_request( $install, $command ){
# Replay token and cookie from valid WP Engine web request
$token = "######";
$cookie = "######";
$curl_url = "https://my.wpengine.com/installs/$install/wp_cli?command=" .urlencode($command);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $curl_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"cookie: $cookie",
"x-csrf-token: $token"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
# convert json output to PHP array
$array = json_decode($response, true);
# output reponse from array back to command line
echo $array["response"];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment