• Hello! I have some promlems with adding new image via xml-rpc(wp.uploadFile).

    public function test ()
     	{
     		$image = 'migrates/image.jpg';
     		$result = $this->addImage($image, 2);
     		echo '<pre>';
     		print_r($result);
     	}
    
    public function addImage ($image, $post_id)
    	{
    		$path = $image;
    		$data = file_get_contents($path);
    		$base64 = 'data:' . $this->mime_content_type($image). ';base64,' . base64_encode($data);
    
    		$content = array(
                'name' => basename($image),
                'type' => $this->mime_content_type($image),
                'bits' => $base64,
                true,
                //'post_id' => $post_id
            );
    
            $context = array(0, $this->wpuser, $this->wppassword, $content);
    
            $xmlcontent = xmlrpc_encode_request('wp.uploadFile', $context, array('encoding'=>'UTF-8','escaping'=>'markup'));
    		return $this->makeRequestAPI($xmlcontent);
    	}
    
    public function makeRequestAPI($xmlcontent)
     	{
     		$arMessage = array();
    
    		$opt = array(
    				'http' => array(
    						'method'  => 'POST',
    						'headers' => "Content-type: text/xml; charset=utf-8 \r\n".
    									 "User-Agent:MyAgent/1.0\r\n".
    									 "Content-length:" . strlen($xmlcontent) . "\r\n",
    						'content' => "$xmlcontent"
    					)
    			);
    		$context = stream_context_create($opt);	
    
    		$fp = fopen($this->targetURL, 'r', false, $context);
    		$result = stream_get_contents($fp);
    
    		$data = xmlrpc_decode($result);
    		if (is_array($data) && xmlrpc_is_fault($data)){
    			$arMessage[] = "Невозможно получить данные";
    			$arMessage[] = "Error Code: " . $data['faultCode'];
    			$arMessage[] = "Error Message: " . $data['faultString'];
    		}else{
    			$arMessage[] = $data;
    		}
    
    		return $arMessage;
    	}

    And my response example to this:

    Array
    (
        [0] => Array
            (
                [attachment_id] => 84
                [date_created_gmt] => stdClass Object
                    (
                        [scalar] => 20160530T12:27:38
                        [xmlrpc_type] => datetime
                        [timestamp] => 1464611258
                    )
    
                [parent] => 0
                [link] => http://wordpress/wp-content/uploads/2016/05/image.jpg
                [title] => image.jpg
                [caption] =>
                [description] =>
                [metadata] =>
                [type] => image/jpeg
                [thumbnail] => http://wordpress/wp-content/uploads/2016/05/image.jpg
                [id] => 84
                [file] => image.jpg
                [url] => http://wordpress/wp-content/uploads/2016/05/image.jpg
            )
    
    )

    But, when I going to admin-media-library my image looks like . I check $base64 before create $context and it was fine(I decode base64 to my image). Can you help me?

  • The topic ‘XML-RPC(wp.uploadFile) problem’ is closed to new replies.