【问题标题】:How can I use cURL's '@' syntax with a remote URL?如何将 cURL 的“@”语法与远程 URL 一起使用?
【发布时间】:2014-03-29 20:52:07
【问题描述】:

我知道这是真的:

...您可以通过在文件路径前加上“@”前缀来发布文件系统上已经存在的文件。

但是,我正在尝试使用非本地 cURL 发布文件。它存储在其他 URL 上。假设这张照片是Google's logo(不是)。它的 URL 是https://www.google.com/images/srpr/logo11w.png。所以我认为你会做这样的事情:

$file = file("https://www.google.com/images/srpr/logo11w.png");
// some more stuff
// define POST data
$post_data = array('somekey' => 'somevalue', 
                    'image' => '@' . $file);

但是,无论出于何种原因,这似乎都不起作用。另外,我尝试使用'image' => '@' . file_get_contents($url)。再一次,这没有用。

看起来解决这个问题的一种方法是使用临时文件。这是解决这个问题的唯一方法吗?无论哪种情况,我该如何解决这个问题?

【问题讨论】:

  • 你的意思是$file = file_get_contents('...');
  • @AmalMurali 我必须将它用于非本地文件吗?
  • 你试过只做$file = "https://www.google.com/images/srpr/logo11w.png";

标签: php file curl


【解决方案1】:

不能使用任何http url 作为 curl 的文件路径。您必须使用本地文件。所以首先将文件下载到一个临时目录中。

file_put_contents("/var/tmp/xyz/output.jpg", file_get_contents("https://www.google.com/images/srpr/logo11w.png"));

然后在你的 curl 中使用这个临时文件:

'image' => '@/var/tmp/xyz/output.jpg'

【讨论】:

  • 是的,你是对的。谢谢!最后一个问题:文件会自动删除还是我需要手动删除?还是会自动覆盖自己?
  • 不会自动删除。下次调用时会自动覆盖。但是在程序结束时,您可以根据需要将其删除。
猜你喜欢
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
相关资源
最近更新 更多