使用php和curl将插件上传到wordpress

时间:2014-07-02 14:42:09

标签: php wordpress curl xml-rpc

我正在尝试在wordpress上传一个插件并激活它。我知道我不应该使用curl和php,但看起来xmlrpc在这种情况下无法帮助我。

所以我有这个功能:

    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;
    }

}

此错误消息是我一直得到的:请选择文件

我真的不知道如何使用php和curl将word插件成功上传到wordpress。

任何建议将不胜感激。 此外,如果有另一种方法可以实现这一点,我也对此持开放态度。

0 个答案:

没有答案