php - 无法将图像从外部 url 复制到我的服务器

标签 php mysql curl fopen file-get-contents

我正在尝试将存储在 mysql 数据中的一堆图像文件链接从一台服务器复制到另一台服务器,这有点像在后台运行的 cron 作业,它检查外部链接并将其复制到服务器。手动查看时显示的链接很少,但当我尝试通过 php 代码复制文件时,没有任何反应。它只输出空字符串。

下面是我尝试复制到我的服务器的一些链接。

  1. http://crm.propspace.com/watermark?c_id=1646&l_id=2300187&aid=1444765&id=14181282100524636&image=09_12_2014-16_53_34-1646-b15d08432d253ac710dc7bea47bf67c9.png
  2. http://crm.propspace.com/watermark?c_id=1646&l_id=2257894&aid=1444765&id=14169037278702926&image=25_11_2014-12_29_24-1646-67974878d8b7abd86d86c93527e15ebd.jpg
  3. http://crm.propspace.com/watermark?c_id=1646&l_id=2257894&aid=1444765&id=14169037278702926&image=25_11_2014-12_29_31-1646-0cf46efe5b2f89214001a2e050353736.jpg
  4. http://crm.propspace.com/watermark?c_id=1646&l_id=2282465&aid=1444765&id=14176053840492751&image=03_12_2014-16_36_29-1646-46cd5f89122e6899f8185e20bd942dd7.png

链接打开并显示图像,但是当我尝试通过 php 代码复制时,我只得到空数据。我尝试了所有类型的方法来复制图像,例如

1. copy()
2. fopen()
3. file_get_contents()
4. curl_init()

但一切都以空字符串或数据结束。谁能帮我解决这个问题。

用于检索图像文件的方法

$img_link       =   "image from link";
$upload_link    =   "image to save link";


Method 1
------------------------------------------------------------------------------
$img_cont       =   file_get_contents($img_link);
file_put_contents($upload_link, $img_cont);



Method 2 - (This method gives me an image with 0 bytes)
------------------------------------------------------------------------------
copy($img_link, $upload_link);


Method 3
------------------------------------------------------------------------------
$img_file   =   fopen($img_link, 'r');
$img_data   =   '';
while($chunk = fread($img_file, 8192))
$img_data   .=  $chunk;
fclose($img_file);


Method 4
------------------------------------------------------------------------------
$img_stream =   fopen($img_link, 'r');
$img_data   =   stream_get_contents($img_stream);
fclose($img_stream);


Method 5
------------------------------------------------------------------------------
$referer    =   "http://www.propspace.com/"
$headers    =   array(
    'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png',
    'Connection: Keep-Alive',
    'Content-type: application/x-www-form-urlencoded;charset=UTF-8'
);

// Initialize cURL Connection
$conn           =   curl_init();

// Set cURL Options
curl_setopt($conn, CURLOPT_URL, $img_link);

// Add Headers
if(is_array($headers) && count($headers) > 0){
    curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
}

// Add References
if($reference != "")
curl_setopt($conn, CURLOPT_REFERER, $reference);

// Other Options
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_BINARYTRANSFER, true);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($conn, CURLOPT_FAILONERROR, true);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($conn, CURLOPT_HEADER, 0);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, 1);

// Execute Request
$img_cont       =   curl_exec($conn);

谢谢。

Update: Just now found a thing which is making the image to show 0 size. It is accessible only from dubai or UAE networks. How can I make it work though?

最佳答案

您尝试下载只有正确登录后才能访问的通用图像。您必须“登录”您的服务器,然后您才能执行此操作。我用 cURL 做过一次.

关于php - 无法将图像从外部 url 复制到我的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601942/

相关文章:

php - 在迷你购物车中隐藏中间破折号(主题 functions.php)

php - 在准备新的 php mysqli stmt 之前什么时候必须关闭它?

php - Symfony/Doctrine 2 ManyToOne关系,ORM不保留列的设置值,发生错误

c++ - 如何在 C++ 中使用 libcurl 发送和接收 POST 请求?

php - PHP 中的 cURL 下载进度不起作用?

php - 尝试将图像及其属性与表单数据一起传递到数组中时出现问题

php - 当 PDO 中没有行时返回值

MySQL 事件从另一个表插入 ID

mysql-workbench - 将 sql 转储恢复到 RDS 的最快方法

php - 使用 CURLOPT_SSLCERT 在 https 服务器上 curl 身份验证错误