Created
September 5, 2017 19:10
-
-
Save julienanquetil/7591c7a657ab33b1abf72c542cb14c1d to your computer and use it in GitHub Desktop.
Magento 2 API add Video to product.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$adminUrl='http://magento2.dev/rest/V1/integration/admin/token'; | |
$ch = curl_init(); | |
$data = array("username" => "admin", "password" => "admin123"); | |
$data_string = json_encode($data); | |
$ch = curl_init($adminUrl); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string)) | |
); | |
$token = curl_exec($ch); | |
$token = json_decode($token); | |
// @Todo : foreach youtube video | |
// get sku + Video ID | |
// | |
$videoId = 'axwE9q7llEQ'; | |
$sku = '123-456' | |
$data = [ 'entry' => [ | |
'id' => null, | |
'media_type' => 'external-video', | |
'label' => null, | |
'position' => 0, | |
'types' => ['image', 'small-image', 'thumbnail'], | |
'disabled' => false, | |
'content' => [ | |
'base64_encoded_data' => base64_encode(file_get_contents('http://img.youtube.com/vi/'.$videoId.'/0.jpg')), | |
'type' => 'image/jpeg', | |
'name' => '0.jpg' | |
], | |
'extension_attributes' => [ | |
'video_content' => [ | |
'media_type' => 'external-video', | |
'video_provider' => 'youtube', | |
'video_url' => 'https://www.youtube.com/watch?v='.$videoId, | |
'video_title' => 'Video', | |
'video_description' => null, | |
'video_metadata' => null | |
] | |
] ] ]; | |
//Use above token into header | |
$headers = array('Content-Type:application/json','Authorization:Bearer '.$token); | |
$ch = curl_init("http://magento2.dev/rest/V1/products/".$sku."/media"); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token)); | |
$response = curl_exec($ch); | |
if ($response == '1'){ | |
echo 'video ajoutee ok' | |
} | |
else { | |
var_dump($response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment