从 URL 发送 cURL 请求?
您好,
我正在寻找一种在给定完整网址的情况下发送卷曲请求的方法。我能找到的所有示例和文档看起来都是这样的:
$fullFilePath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
'photo'=>"@$fullFilePath",
'title'=>$title
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);
问题是文件“test.jpg”实际上是由服务器上的脚本动态生成的(因此文件系统上不存在)。
我如何发送请求,而不是使用 $file = "http://www.mysite.com/generate/new_image.jpg"
我想到的一种解决方案是使用 fopen 或 file_get_contents() 将“new_image.jpg”加载到内存中但一旦我到达这一点,我不知道如何将其作为 POST 发送到另一个网站
Greetings,
I'm looking for a way to send a curl request given a full url. All of the examples and documentation I can find look something like this:
$fullFilePath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
'photo'=>"@$fullFilePath",
'title'=>$title
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);
The problem is that the file, "test.jpg" is actually generated dynamically by a script on the server (so it doesn't exist on the file system).
How can I send the request, instead using $file = "http://www.mysite.com/generate/new_image.jpg"
One solution that came to mind was loading "new_image.jpg" into memory with fopen or file_get_contents() but once I get to that point I'm not sure how to send it as POST to another site
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,最简单的解决方案是将文件写入临时位置,然后在 cURL 请求完成后将其删除:
请注意,我插入一个随机数以避免竞争条件。如果您的图像不是特别大,最好在文件名中使用
md5($img)
而不是rand()
,这样可以 仍然会导致碰撞。By far the easiest solution is going to be to write the file to a temporary location, then delete it once the cURL request is complete:
Note that I am inserting a random number to avoid race conditions. If your image is not particularly big, it would be better to use
md5($img)
in your filename instead ofrand()
, which could still result in collisions.