[英]How to copy file without extension from remote url (Google Play Store image of app) to server's temp folder in php?
I am working on application of scraping structured data from Google Playstore and saving it in the database. 我正在研究从Google Playstore抓取结构化数据并将其保存在数据库中的应用程序。 I am getting all the structured data.
我正在获取所有结构化数据。 I am trying to copy the
itemprop="image"
url and save the image to my server. 我正在尝试复制
itemprop="image"
网址并将图像保存到我的服务器。
I have tried many things but nothing works as the file doesn't have an extension. 我已经尝试了很多方法,但是没有任何作用,因为该文件没有扩展名。 The code below works but the either invalid file is generated or a file with zero bytes is created.
下面的代码有效,但是要么生成了无效文件,要么创建了零字节文件。
Sample url that I am trying to copy 我要复制的示例网址
https://lh3.googleusercontent.com/IYZe0LQOUKXpEYOyVOYYMJo4NnqBnDYkkhDgfYTgDCpuxAyy1ziBkOn0b6_LZxQ3qI4=w300-rw
PHP code that I am using 我正在使用的PHP代码
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $useragent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER,false);
// curl_setopt($process, CURLOPT_SSL_VERIFYPEER,0);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$image="https://lh3.googleusercontent.com/IYZe0LQOUKXpEYOyVOYYMJo4NnqBnDYkkhDgfYTgDCpuxAyy1ziBkOn0b6_LZxQ3qI4=w300-rw";
$upload_dir = wp_upload_dir();
$length= strlen($image);
$new_string = substr($image,0,$length-8);
$imagename= basename($new_string);
$image2 = getimg($imgurl);
$new_image_path = file_put_contents($upload_dir['basedir'].'/custom-temp/'.$imagename,$image2);
There are several errors in your code such as the $useragent
not being named consistently and $imgurl
being undefined. 您的代码中存在几个错误,例如
$useragent
名称不一致,而$imgurl
的名称未定义。 Consider turning on errors in PHP when you are debugging so that you can catch these. 考虑在调试时打开PHP中的错误,以便可以捕获这些错误。
I can get your code to work like this (just change the path back to wp_upload_dir()
): 我可以使您的代码像这样工作(只需将路径更改回
wp_upload_dir()
):
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$gim = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
// curl_setopt($process, CURLOPT_USERAGENT, $useragent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER,false);
// curl_setopt($process, CURLOPT_SSL_VERIFYPEER,0);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$image="https://lh3.googleusercontent.com/IYZe0LQOUKXpEYOyVOYYMJo4NnqBnDYkkhDgfYTgDCpuxAyy1ziBkOn0b6_LZxQ3qI4=w300-rw";
$upload_dir = "/home/laurent/test/upload";
$length= strlen($image);
$new_string = substr($image,0,$length-8);
$imagename= basename($new_string);
$image2 = getimg($image);
$new_image_path = file_put_contents($upload_dir.'/'.$imagename,$image2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.