我试图通过REST API将文件_file_get_contents转换为jpeg文件,因为我需要它通过REST API将其上传到我的Magento网站。
$REMOTE_FILE_URL = "https://www.natures-collection.com/wp-content/uploads/2020/04/a389964a-dfba-48ad-81ba-09e639bc436a-450x450.jpg";
$ax = file_get_contents($REMOTE_FILE_URL);
$aa = base64_encode($ax);
d($alld['image'], $REMOTE_FILE_URL, $ax, $aa);
$basename = basename($alld['image']);
$ext = explode(".",$basename)[1];
if ($ext === "jpg") {
$ext = "jpeg";
}
d($basename,$ext);
$jso_aa = <<<EOT
{
"entry": {
"media_type": "image",
"label": "I am an image!",
"types": [
"image",
"small_image",
"thumbnail",
"swatch"
],
"content": {
"base64_encoded_data": "$aa",
"type": "image/{$ext}",
"name": "{$basename}"
},
"file": "{$basename}"
}
}
EOT;
d($jso_aa);
$reqq0 = <<<EOT
curl -X POST "http://167.179.118.154/index.php/rest/default/V1/products/{$sku}/media" -H "Authorization: Bearer joax1huoa36e0b80thbx6zynmmrthnlq" -H "Content-Type:application/json" -d '{$jso_aa}'
EOT;
d()是一个自定义函数,其作用类似于var_dump()。
但是,file_get_contents函数返回false。当我转储$ ax时,我看到FALSE。
此URL发生这种情况,在浏览器上可以正常工作。
另一个如下网址,没有这个问题:https://www.natures-collection.com/wp-content/uploads/2020/04/ea588379-eb43-4adb-a9d9-a3475ce7b2be.jpg
为什么会这样,我该如何解决?
演示:https://repl.it/@kallefrombosnia/filegetcon
$REMOTE_FILE_URL = "https://www.natures-collection.com/wp-content/uploads/2020/04/a389964a-dfba-48ad-81ba-09e639bc436a-450x450.jpg";
$options = [
'http' => [
'method' => 'GET',
'header' => 'User-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3219.0 Safari/537.36',
]
];
$context = stream_context_create($options);
$ax = file_get_contents($REMOTE_FILE_URL, false, $context);
// just test that it actually works fine
file_put_contents('test.jpg', $ax);
// below is your code