使用REST API在wordpress上传图像

时间:2015-07-01 10:43:15

标签: php wordpress

使用rest api我可以创建帖子,获取博客类别等但我无法上传我所指的图像 https://github.com/WP-API/client-php/blob/master/library/WPAPI/Media.php

http://wp-api.org/#entities_media-meta_width

我的代码是

$data = array('file'=>$filePath,'is_image'=>true);
print_r($data);

$headers = array('Content-Type' => 'application/octet-stream');
$response = $this->api->post(WPAPI::ROUTE_MEDIA, $headers, $data);

他们谈论[$ response in $ response = $ this-> api-> post(WPAPI :: ROUTE_MEDIA,$ headers,$ data);] $ data中使用的键值对是什么?

3 个答案:

答案 0 :(得分:5)

最后我自己解决了

  1. 指定文件路径
  2. 放入必要的标题
  3. 发送帖子请求
  4. 简单的确定我将在

    下面显示代码
    $filePath = 'URL of image';//right click your fav img and copy url.
    $theData = @file_get_contents($filePath);//get image content vvvvvip 
    $data = $theData;
    $headers = array('Content-Type' => 'application/json; charset= UTF-8',
           'Content-Disposition' => 'attachment; file name=' .base name($filepath),);
    $response = $this->api->post('/wp-json/media', $headers,$data);
    print_r($response);
    

    //在wordpress媒体中查看它 享受..

答案 1 :(得分:1)

试试这个。

1 open chrome browser.
2 install apps (https://chrome.google.com/webstore/search/rest%20client?utm_source=chrome-ntp-icon)
3 open this apps and use
  

Screenshort

enter image description here

答案 2 :(得分:-1)

  

这是我的API代码

   <?php
        require_once("../wp-load.php");
        global $wpdb;
        $name = urldecode($_POST['name']);
        $city = urldecode($_POST['city']);
        $country = urldecode($_POST['country']);
        $email = urldecode($_POST['email']);
        $password = urldecode($_POST['password']);
        $confirm_password = urldecode($_POST['confirmpassword']);
        $number = urldecode($_POST['number']);
        $job_type = urldecode($_POST['jobtype']);
        $job_exp = urldecode($_POST['exp']);
        $job_location = urldecode($_POST['job_location']);
        $bod = urldecode($_POST['dob']);
        $facebook_url = urldecode($_POST['fburl']);
        $twitter_url = urldecode($_POST['twurl']);
        $googleplus_url = urldecode($_POST['googleurl']);
        $dribble_url = urldecode($_POST['dribbleurl']);
        $pinterest_url = urldecode($_POST['pinteresturl']);
        $linkedin_url = urldecode($_POST['linkdinurl']);
        $description = urldecode($_POST['description']);
        $job_skills = urldecode($_POST['job_skill']);
        $job_degree = urldecode($_POST['degree']);
        $career_level = urldecode($_POST['level']);
        $skills_array = explode(",",$job_skills);   
        $ser_skill = serialize($skills_array);
        $get_extension = explode(",",$user_resume);
        $mim = $get_extension[0];
        $secondEx = explode(": ",$mim);
        $minSec = $secondEx[1];
        $FinalEx = explode(";",$minSec);
        $key = $FinalEx[0];
        if($name != "" &&   $city != "" &&  $country != "" &&   $email != "" && $password != "" &&  $number != "" &&    $job_type != "" &&  $job_exp != "" &&   $job_location != "" &&  $bod != "" &&   $facebook_url != "" &&  $twitter_url != "" &&   $googleplus_url != "" &&    $dribble_url != "" &&   $pinterest_url != "" &&     $linkedin_url != "" &&  $description != "" &&   $job_skills != "" &&    $job_degree != "" && $career_level != "")
        {
            if(username_exists( $email ))
            {
                $pre = '{"status": "0"';
                $postd = ',"message": "User Exist" }';
                echo $finalJson = json_pretty($pre.$postd);
                exit;
            }
            if(email_exists( $email ))
            {
                $pre = '{"status": "0"';
                $postd = ',"message": "Email Exist" }';
                echo $finalJson = json_pretty($pre.$postd);
                exit;
            }
            if($password == $confirm_password)
            {
                if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
                $uploadedfile = $_FILES['user_photo'];
                /******* user_photo Upload code ************/
                $upload_overrides = array( 'test_form' => false );
                $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
                $photo_url = $movefile['url'];
                /****** resume Upload code ***********/
                $uploadedResume = $_FILES['user_resume'];
                $upload_overrides = array( 'test_form' => false );
                $moveResume = wp_handle_upload( $uploadedResume, $upload_overrides );
                $resume_Url = $moveResume['url'];
                /*****************************************/ 
                $userdata = array('user_login' => $email,'user_pass' =>  $password, 'user_email' =>  $email,'role'=> 'jobseeker','first_name'=>$name,'description'=>$description);
                $user_id = wp_insert_user( $userdata );
                add_user_meta($user_id,'full_name',$name);
                add_user_meta($user_id,'city',$city);
                add_user_meta($user_id,'country',$country);
                add_user_meta($user_id,'mobile_no',$number);
                add_user_meta($user_id,'job_type',$job_type);
                add_user_meta($user_id,'_experience',$job_exp);
                add_user_meta($user_id,'job_location',$job_location);
                add_user_meta($user_id,'_bod',$bod);
                add_user_meta($user_id,'_job_skill',$skills_array);
                add_user_meta($user_id,'_fburl',$facebook_url);
                add_user_meta($user_id,'_twurl',$twitter_url);
                add_user_meta($user_id,'_gpurl',$googleplus_url);
                add_user_meta($user_id,'_dribbleurl',$dribble_url);
                add_user_meta($user_id,'_pinteresturl',$pinterest_url);
                add_user_meta($user_id,'_linkedinurl',$linkedin_url);
                add_user_meta($user_id, 'emp_photo',$photo_url);
                add_user_meta($user_id, 'emp_resume',$resume_Url);
                add_user_meta($user_id, 'emp_degree',$job_degree);
                add_user_meta($user_id, 'emp_level',$career_level);
                add_user_meta($user_id, 'user_subscription','basic');
                add_user_meta($user_id, 'subscription_pack', 'silver');
                if(!empty($user_id))
                {
                    $pre = '{"status": "1"';
                    $postd = ',"message": "Success Register Jobseeker" }';
                    echo $finalJson = json_pretty($pre.$postd); 
                    exit;
                }
            }
            else{
                $pre = '{"status": "0"';
                $postd = ',"message": "Password and Confirm Password Does not match." }';
                echo $finalJson = json_pretty($pre.$postd);
                exit;
            }
        }else
        {
            $pre = '{"status": "0"';
            $postd = ',"message": "All Field are Requier" }';
            echo $finalJson = json_pretty($pre.$postd); 
            exit;
        }
    function json_pretty($json, $html = false) {
    $out = ''; $nl = "\n"; $cnt = 0; $tab = 4; $len = strlen($json); $space = ' ';
    if($html) {
        $space = '&nbsp;';
        $nl = '<, $html = false)br/>';
    }
    $k = strlen($space)?strlen($space):1;
    for ($i=0; $i<=$len; $i++) {
        $char = substr($json, $i, 1);
        if($char == '}' || $char == ']') {
            $cnt --;
            $out .= $nl . str_pad('', ($tab * $cnt * $k), $space);
        } else if($char == '{' || $char == '[') {
            $cnt ++;
        }
        $out .= $char;
        if($char == ',' || $char == '{' || $char == '[') {
            $out .= $nl . str_pad('', ($tab * $cnt * $k), $space);
        }
    }
    return $out;
    }
    ?>