1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Upload plugin to wordpress with php and curl

Discussion in 'Other Languages' started by handmadebots, Jul 13, 2014.

  1. handmadebots

    handmadebots Senior Member

    Joined:
    Nov 8, 2012
    Messages:
    904
    Likes Received:
    204
    Home Page:
    I am trying to upload a plugin to my wordpress using php and curl, but it looks like everything I tried is just not working.

    Here is my code:
    Code:
    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;
        }
    
    }
    
    I also asked the same question on stackoverflow, but no response.
    Here is the link: http://stackoverflow.com/questions/24534103/upload-plugin-to-wordpress-with-php-and-curl

    Any ideas from the php pros ?
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    950
    Likes Received:
    662
    Occupation:
    Web/Bot Developer
    Wow such complications. It would be much simpler to use SSH + SCP to upload and extract the files into your plugin folder.

    Example
    Code:
    # move plugin folder tarball to server
    scp pluginfiles.tar username@123.123.123.123:/path/to/your/wp/plugin/folder/
     
    # untar files into wp plugin folder
    ssh username@123.123.123.123 tar zxvf /path/to/your/wp/plugin/folder/pluginfiles.tar -C /path/to/your/wp/plugin/folder/
    
    You can also call these shell commands directly from PHP using shell_exec
    Code:
    <?php
    shell_exec('scp pluginfiles.tar username@123.123.123.123:/path/to/your/wp/plugin/folder/');
    ?>
    
     
    Last edited: Sep 13, 2014