简体   繁体   English

使用PHP curl将URL作为文件上传

[英]Upload a URL as a file using PHP curl

I have images that are hosted on a CDN, and I want to upload that image to some other system via their API. 我有托管在CDN上的映像,我想通过其API将映像上传到其他系统。 Normally I would just use curl and put an @ character in front of the file to upload. 通常,我只会使用curl并将@字符放在要上传的文件前面。 Since this is a URL, that won't work since it expects the file to be local. 由于这是一个URL,因此无法正常工作,因为它希望该文件位于本地。 I don't want to copy it local first since the files could be videos and that could be time consuming. 我不想先将其复制到本地,因为文件可能是视频,这可能很耗时。

It seems there should be a way to do this with PHP curl. 似乎应该有一种使用PHP curl做到这一点的方法。 I'm open to using sockets instead, but I'm not sure how to simulate how curl does the submission. 我愿意使用套接字,但是我不确定如何模拟卷曲如何提交。

<?php

$save_file_url =  file_get_contents($your_files_CDN_file_URL);

$fp = fopen('test','w');
fwrite($fp,$save_file_url);
$save_file = realpath('test') ;

$url = "http://URL_TO_SEND_FILE/get_file.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    "file_is_this"=> "@".$save_file,
    "app"=>'test'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);

?>

And you will recieve file at get_file.php. 您将在get_file.php接收文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM