如何使用cURL的'@'语法与远程URL? [英] How can I use cURL's '@' syntax with a remote URL?

查看:155
本文介绍了如何使用cURL的'@'语法与远程URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是真的:


...您可以通过在文件路径前面添加一个已经在文件系统上的文件@。

... you can post a file that is already on the filesystem by prefixing the filepath with "@".

但是,我试图使用非本地的cURL POST文件。它存储在一些其他URL。让我们说这张照片是 Google标志(不是)。其网址为 https://www.google.com/images/srpr/logo11w.png 。所以我认为你会这样做:

However, I'm trying to POST a file with cURL that's not local. It's stored on some other URL. Let's just say this photo is Google's logo (it isn't). The URL of that is https://www.google.com/images/srpr/logo11w.png. So I would think that you would do something like this:

$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)。再次,这不工作。

However, this doesn't seem to work for whatever reason. Also, I tried using 'image' => '@' . file_get_contents($url). Again, that didn't work.

看起来像一种方法来解决这是使用一个临时文件。这是唯一的解决这个问题吗?

It looks like a way to get around this is to use a temporary file. Is this the only solution to this problem? In either case, how can I solve this problem?

推荐答案

不能使用任何 http url为curl的文件路径。你必须使用本地文件。因此,首先将文件下载到临时目录中。

You can not use any http url for the file path at curl. You have to use local file. So first download the file into a temporary directory.

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

然后将此临时文件用于curl:

Then use this temporary file into your curl:

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

这篇关于如何使用cURL的'@'语法与远程URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆