Upload plugin to wordpress with php and curl

I am trying to upload a plugin on wordpress and activate it. I know I shouldn’t use curl and php for this, but it looks like the xmlrpc won’t be able to help me in this case.

So I have this function:

Read More
    function uploadPlugin($filenameWithFullPath) {
        if (!function_exists('curl_init') || !function_exists('curl_exec')) {
            echo 'curl is not available!';
        }

        $visitHost = urlencode($this->wpAdminUrl . '/plugin-install.php?tab=upload');
        $postHost = $this->wpAdminUrl . '/update.php?action=upload-plugin';

        //$postData = "log=$username&pwd=$password&wp-submit=Log%20In&redirect_to=$visitUr";

        $postData = "log=$this->username&pwd=$this->password&wp-submit=Log+In&redirect_to=$visitHost";

        //echo $postData . '<br><br>';

        $ch = curl_init();

        //Url to use for login
        curl_setopt($ch, CURLOPT_URL, $this->loginUrl);

        //Activate cookiesession

        curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
        //Set cookie
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile);

        //No need for SSL
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        //User agent
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);

        //Timeout
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        //Authentication
        curl_setopt($ch, CURLOPT_USERPWD, 'skywp:119wp');

        //Follow redirection
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        //Return or echo
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //Referer
        curl_setopt($ch, CURLOPT_REFERER, $this->loginUrl);

        //POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_POST, 1);

        //Save result to content
        $content = curl_exec($ch);
        //echo $content;

        $matches = array();

        $wponce = "";
        if (preg_match('#id="_wpnonce" name="_wpnonce" value="(.*)" /><input type="hidden"#', $content, $matches)) {
            $wponce = $matches[1];
        }

        $matches = array();
        $wpreferer;
        if (preg_match('#<input type="hidden" name="_wp_http_referer" value="(.*)" />#', $content, $matches)) {
            $wpreferer = $matches[1];
        }

        curl_setopt($ch, CURLOPT_URL, $postHost);

        //Interesting thing. The parameter install-plugin-submit shows in firebug but doesn't show up with live http headers
        //Without extra parameter
        //$postData = "_wpnonce=$wponce&_wp_http_referer=$wpreferer&pluginzip=$filenameWithFullPath";
        //With extra parameter

        $cfile = curl_file_create($filenameWithFullPath);
        $postData2 = array(
            //'pluginzip' => '@' . $filenameWithFullPath,
            'pluginzip' => $cfile,
            '_wp_http_referer' => $wpreferer,
            '_wpnonce' => $wponce,
            'install-plugin-submit' => 'Install Now'
        );
        //$postData2 = "_wpnonce=$wponce&_wp_http_referer=$wpreferer&pluginzip=@$filenameWithFullPath&install-plugin-submit=Install Now";

        //curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData2));
        curl_setopt($ch, CURLOPT_POST, 1);

        $content = curl_exec($ch);
        //var_dump( $content );
        //Close curl
        curl_close($ch);

        return $content;
    }

}

This error message is what I get all the time: Please select a file

I really have no idea how to upload a plugin with success to wordpress using php and curl.

Any suggestions will be appreciated.
Also if there is another way to achieve this, I am open to that too.

Related posts

Leave a Reply