Upload plugin wordpress with php and curl

Viewed 195

I'm trying to install plugins remotely to my wordpress sites. I've tried something but wordpress gives an error message "Please Select A File", I have already posted a plugin file using curl. I need some help!

I get

Please select a file

When I add CURLOPT_HTTPHEADER multipart/form-data i get

403 not authorize

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    define("ROOT_PATH",$_SERVER['DOCUMENT_ROOT']);
    
    
    $username = 'testuser'; //wp user name
    $password = 'testpassword'; //wp password
    $url = "https://example.com"; /// without / at the end
    $cookie = ROOT_PATH.'/include/cookies.txt';
    $visitHost = $url . '/wp-admin/plugin-install.php?tab=upload';
    $postHost =  $url . '/wp-admin/update.php?action=upload-plugin';
    $postData = "log=" . $username . "&pwd=" . $password . "&wp-submit=Log+In" . "&redirect_to=" . $url . "/wp-admin/plugin-install.php?tab=upload" . "&testcookie=1";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . "/wp-login.php");
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_POST, 1);
    $content = curl_exec($ch);
    $matches = array();
    $wponce = "";
    if (preg_match('#id="_wpnonce" name="_wpnonce" value="(.*)" /><input type="hidden"#', $content, $matches)) {
        $wponce = $matches[1];
    }
    
    $matches = array();
    //$wpreferer = "/wp-admin/plugin-install.php";
    if (preg_match('#<input type="hidden" name="_wp_http_referer" value="(.*)" />#', $content, $matches)) {
        $wpreferer = $matches[1];
    }
    
    curl_setopt($ch, CURLOPT_URL, $postHost);
    $filename = ROOT_PATH.'/include/wp-settings.zip';
    $mimetype = "application/zip";
    $cfile = curl_file_create($filename, $mimetype, basename($filename));
    $postData2 = array(
        'pluginzip' => $cfile,
        '_wp_http_referer' => "/wp-admin/plugin-install.php",
        '_wpnonce' => $wponce,
        'install-plugin-submit' => 'Install Now'
    );
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData2));
    curl_setopt($ch, CURLOPT_POST, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    
    echo $content;
    ?>
0 Answers
Related