How to save a gif image from a Vkontakte post to the server? PHP
How to save for example this .gif image to your server? https://psv4.userapi.com/c816137/u17148100/docs/838904d1498e/Bob.gif
My non-working code:
$url = 'https://psv4.userapi.com/c816137/u17148100/docs/838904d1498e/Bob.gif';
$path = 'image.gif';
file_put_contents($path, file_get_contents($url));
1
Author: Coffee inTime, 2018-08-24
1 answers
copy($url, '/path/to/file.gif')
Documentation: copy
.
In case the first option didn't work, you can try this:
<?php
call($url); // кидает запрос на указанный URL с пользовательским
// юзерагентом, без этого для ВК не работает.
if (copy($url), './test.gif')) {
echo 'true';
} else {
print_r( error_get_last() );
}
function call($url, $post = null, $_hash = null) {
if( $curl = curl_init() ) {
curl_setopt($curl, CURLOPT_URL, $url);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36');
if($post) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post));
}
$out = curl_exec($curl);
curl_close($curl);
return ($out);
}
}
?>
1
Author: ya.ymer, 2018-08-24 08:39:27