So I have an image uploaded in Vue JS and I send the POST request to a Laravel Route. The image gets uploaded, saved on my local and sent to an API. The API request returns its JSON response on client side (as expected) BUT I cannot do anything with this response on PHP, it returns null in PHP. I can only see the JSON response client side, which is weird. I have been trying to figure this out for a couple of days.
Here is my Vue JS code that uploads a picture:
let token = document.head.querySelector('meta[name="csrf-token"]');
export default{
data(){
return {
image: ''
}
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.createImage(files[0]);
},
createImage(file) {
let reader = new FileReader();
let vm = this;
reader.onload = (e) => {
vm.image = e.target.result;
};
reader.readAsDataURL(file);
},
upload(){
let formData = new FormData();
formData.append('image', this.image);
console.log(this.image) ;
axios.post( 'nova-vendor/ad-asset-upload/add',
formData,
{
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': token.content
}
}
).then(function (response){
console.log(response);
})
.catch(function(){
console.log('FAILURE!!');
});
},
}
}
Vue JS returns a base64 image and I convert it in the PHP controller to jpg or png and it uploads to the API / locally. I see the response on client side like this:
{"error":false,"size":27410,"filename":"yJCH2T","ext":"png","url":"https://vgy.me/u/yJCH2T","image":"https://vgy.me/yJCH2T.png","delete":"https://vgy.me/delete/eGGhib4gPY2a"}
In PHP no matter what I do I cannot get the JSON data, BUT I CAN get the JSON when I replicate my steps on vanilla php without Larval or Vue.
Here is my PHP code once the base64 string is converted into an image.
$ch = curl_init('https://vgy.me/upload');
$file = new CURLFile($file, $type, $filename );
$data = array('file' => $file, 'title' => 'Beautiful Flowers', 'description' => 'A beautiful photo of flowers at the local park.');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$ok = curl_exec($ch);
$json = json_decode($ok, true);
So I would expect$json['image']
to return the image url from JSON string, but its null.
In case you were wondering about my image conversion logic here it is...
$data = $request->input('image');
$pos = strpos($data, ';');
$type = explode(':', substr($data, 0, $pos))[1];
$folderPath = "storage/";
$image_parts = explode(";base64,", $request->input('image'));
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
if ( $type == "image/jpeg" || $type == "image/jpg"){
$file = $folderPath . uniqid() . '.jpg';
$filename = uniqid() . '.jpg';
} else if ( $type == "image/png" )
{
$file = $folderPath . uniqid() . '.png';
$filename = uniqid() . '.png';
}
$img = file_put_contents($file, $image_base64);
I figured it out by adding
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
to my CURL code. But now I guess I have to go back and rework my original Guzzle Package I created that works in just regular old PHP / HTML.