Php 从URL发送cURL请求?

Php 从URL发送cURL请求?,php,curl,dynamic-image-generation,Php,Curl,Dynamic Image Generation,您好 我正在寻找一种方法,在给定完整url的情况下发送curl请求。我能找到的所有示例和文档如下所示: $fullFilePath = 'C:\temp\test.jpg'; $upload_url = 'http://www.example.com/uploadtarget.php'; $params = array( 'photo'=>"@$fullFilePath", 'title'=>$title ); $ch = curl_init(); cu

您好

我正在寻找一种方法,在给定完整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发送到另一个站点

到目前为止,最简单的解决方案是将文件写入临时位置,然后在cURL请求完成后将其删除:

// assume $img contains the image file
$filepath = 'C:\temp\tmp_image_' . rand() . '.jpg'
file_put_contents($filepath, $img);
$params = array(
    'photo'=>"@$filepath",
    'title'=>$title
);    
// do cURL request using $params...

unlink($filepath);

请注意,我插入了一个随机数以避免竞争条件。如果您的图像不是特别大,最好在文件名中使用
md5($img)
,而不是
rand()
,因为仍然会导致冲突。

此方法有效,我希望避免保存副本,但这似乎是我唯一的选择。感谢您的帮助-@pws5068这是可能的,但它基本上需要手动构建帖子。你可能不想那样做。