admin管理员组

文章数量:1297010

I need your help to upload a media image in my wordpress blog via the Wp-rest-api v2 and Oauth2 authentication.

I did not find in the REST API documentation the way to send my image data (name of field, sending mode...?).

require('OAuth2/Client.php');
require('OAuth2/GrantType/IGrantType.php');
require('OAuth2/GrantType/AuthorizationCode.php');

const CLIENT_ID     = 'XXX';
const CLIENT_SECRET = 'XX';

const REDIRECT_URI           = 'http://127.0.0.1/test_api_wp/test.php';

const AUTHORIZATION_ENDPOINT = '/oauth/authorize';
const TOKEN_ENDPOINT         = '/oauth/token';

$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);

if (!isset($_GET['code']))
{
    $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
    header('Location: ' . $auth_url);
    die('Redirect');
}
else
{
    $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
    $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); //authorization_code
    $token = $response['result']['access_token'];
    $client->setAccessToken($token);
    $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER);

}

$values = array(
    "date" => "2015-11-26 10:00:00",
    "date_gmt" => "2015-11-26 09:00:00",
    "modified" => "2015-11-26 10:00:00",
    "modified_gmt" => "2015-11-26 09:00:00",
    "status" => "future",
    "title" => "Titre media",       
    "description" => "description media",
    "media_type" => "image",
    "source_url" => ".png"
);

$data = $client->fetch("wordpress.local/wp-json/wp/v2/media", $values, "POST");
echo "<pre>";print_r($data);echo "</pre>";

The response :

Array
(
    [result] => Array
        (
            [code] => rest_upload_no_data
            [message] => No data supplied
            [data] => Array
                (
                    [status] => 400
                )

        )

    [code] => 400
    [content_type] => application/json; charset=UTF-8
)

Any idea? Thanks a lot

I need your help to upload a media image in my wordpress blog via the Wp-rest-api v2 and Oauth2 authentication.

I did not find in the REST API documentation the way to send my image data (name of field, sending mode...?).

require('OAuth2/Client.php');
require('OAuth2/GrantType/IGrantType.php');
require('OAuth2/GrantType/AuthorizationCode.php');

const CLIENT_ID     = 'XXX';
const CLIENT_SECRET = 'XX';

const REDIRECT_URI           = 'http://127.0.0.1/test_api_wp/test.php';

const AUTHORIZATION_ENDPOINT = 'http://wordpress.local/oauth/authorize';
const TOKEN_ENDPOINT         = 'http://wordpress.local/oauth/token';

$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);

if (!isset($_GET['code']))
{
    $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
    header('Location: ' . $auth_url);
    die('Redirect');
}
else
{
    $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
    $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); //authorization_code
    $token = $response['result']['access_token'];
    $client->setAccessToken($token);
    $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER);

}

$values = array(
    "date" => "2015-11-26 10:00:00",
    "date_gmt" => "2015-11-26 09:00:00",
    "modified" => "2015-11-26 10:00:00",
    "modified_gmt" => "2015-11-26 09:00:00",
    "status" => "future",
    "title" => "Titre media",       
    "description" => "description media",
    "media_type" => "image",
    "source_url" => "https://www.base64-image.de/build/img/mr-base64-482fa1f767.png"
);

$data = $client->fetch("wordpress.local/wp-json/wp/v2/media", $values, "POST");
echo "<pre>";print_r($data);echo "</pre>";

The response :

Array
(
    [result] => Array
        (
            [code] => rest_upload_no_data
            [message] => No data supplied
            [data] => Array
                (
                    [status] => 400
                )

        )

    [code] => 400
    [content_type] => application/json; charset=UTF-8
)

Any idea? Thanks a lot

Share Improve this question edited Nov 26, 2015 at 10:08 cybmeta 20.6k5 gold badges47 silver badges57 bronze badges asked Nov 26, 2015 at 8:18 kain34440kain34440 1231 gold badge1 silver badge4 bronze badges 6
  • I've added the coded from your comment to the question. Remember that you can edit the question at any time to add more information or make it more clear. – cybmeta Commented Nov 26, 2015 at 9:30
  • The extra ; in wordpress.local/wp-json/wp/v2/media";is a mistype here or it is in your real code too? – cybmeta Commented Nov 26, 2015 at 9:33
  • According with docs, WP REST API v2 requires this OAuth plugin. I don't know if the library that you are using (OAuth2/Client.php) is compatible with WP REST API or not, but it is probably not. – cybmeta Commented Nov 26, 2015 at 9:45
  • Thanks a lot !The extra --> ; is not present in my real code! I use official OAuth plugin to authenticate me, files OAuth2.Client.php is only library to make curl requests easily – kain34440 Commented Nov 26, 2015 at 9:51
  • There is a Create a Media section in (documentation)[v2.wp-api/reference/media/]. I think your source_url should be inside post object. – ville6000 Commented Nov 26, 2015 at 11:09
 |  Show 1 more comment

3 Answers 3

Reset to default 11

SO! This is fun.

Keep in mind the WP-API is still very, very much a work-in-progress.

Content-Disposition

I found an issue reported on the WP-API issue queue about Content-Disposition. This is a required header for posting new media content and there are some very, very strict requirements when it comes to providing this in the proper format.

The Purpose of Create Media Endpoint

First, Let's take a step back. The API assumes at this point you have already uploaded a new file to the correct directory. This endpoint is creating the media content in the database that references this file.

The solution

You have to specify the filename of the media file to associate to your new content. This cannot be a remote url. As you can see from the v2 documentation, source_url and link are read-only. All you have to do to successfully submit your new content is add the following to your header:

'Content-Disposition' => 'filename=name-of-file.jpg',

As mentioned in the ticket, you cannot add quotes or specify the method you're using to send the file. It must be in the format above. At least, this is the case until they change it all around.

Make sure the file type is one of the accepted file types and you're including the extension of the file is included in the request. Thanks to Dr Deo in the comments.

For the record, I laughed with giddy joy when I finally figured this one out... scared the hell out of my wife.

For the sake of "cross-referencing", see my related answer here on StackOverflow about media upload and using that media as "featured media" for a post.

The answer for PHP CURL:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'EndPoint',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('file'=> new CURLFILE('LocalFilePathHere')),
  CURLOPT_HTTPHEADER => array(
    'Content-Disposition: attachment; filename=abc.jpg',
    'Content-Type: image/jpeg',
    'Authorization: XXX YYY'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

If you still face issue "Sorry, this file type is not permitted for security reasons", you have to add to wp-config.php to disable Fiter for Admin User

define('ALLOW_UNFILTERED_UPLOADS', true);

本文标签: Add media with WPRestAPI v2