简体   繁体   English

PHP cURL发送和接收图像客户端/服务器

[英]PHP cURL sending and receive Images Client / Server

I have been researching this for a while and have not been find an answer for this. 我已经研究了一段时间,但没有找到答案。

I have a Client Site making calls to our API Server. 我有一个客户站点正在调用我们的API服务器。 What I would like to transfer an image to the Client Site when a special call is made. 拨打特殊电话后,我想将图像传输到客户站点。

I have some code that downloads the image from the server, but this is causing us to make multiple calls forcing us to create all these images in the server that we don't want to keep, even if we delete them afterward. 我有一些代码可以从服务器下载图像,但这导致我们进行多次调用,迫使我们在服务器上创建所有我们不希望保留的图像,即使我们随后将其删除也是如此。



$originalFileUrl = createImage('createImage', $fileName);
downloadImage($originalFileUrl, $fileDestination);
deleteFileFromServer('deleteImage', $fileName);


function serverCall ($action, $fileName) {

      $serverCall = $SERVER.'/api.php?fileName=' . $fileName . '&action=' . $action;

      ob_start();
      $ch = curl_init();
      $timeout = 5; 

      curl_setopt ($ch, CURLOPT_URL, $serverCall);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      curl_exec($ch);

      $fileContents = ob_get_contents();

      curl_close($ch);
      ob_end_clean();

      return $fileContents;
}

function downloadImage ($originalFileUrl, $fileDestination) {      
      // Starting output buffering
      ob_start();

      // create a new CURL resource
      $ch = curl_init();

      // set URL and other appropriate options
      curl_setopt($ch, CURLOPT_URL, $originalFileUrl);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

      // set timeouts
      set_time_limit(30);                     // set time in secods for PHP
      curl_setopt($ch, CURLOPT_TIMEOUT, 30);  // and also for CURL

      // open a stream for writing
      $outFile = fopen($fileDestination, 'wb');

      curl_setopt($ch, CURLOPT_FILE, $outFile);

      // grab file from URL
      curl_exec($ch);
      fclose($outFile);

      // close CURL resource, and free up system resources
      curl_close($ch);  
      ob_end_clean();
}

Where $originalFileUrl is the current location of the file, and $fileDestination is the path to where I want my new file to be. 其中$ originalFileUrl是文件的当前位置,而$ fileDestination是我希望新文件所在的路径。

My question is: Can I make a call to a PHP file in the Server that will be in charge of create, transfer and delete the image all in one call rather than doing multiple calls? 我的问题是:我可以一次调用服务器中负责创建,传输和删除映像的PHP文件,而不是多次调用吗?

Also for multiple reasons ftp the file from the server to the client is not a good option. 同样由于多种原因,将文件从服务器ftp到客户端ftp不是一个好的选择。

Thank you 谢谢

This will not be a trivial task. 这将不是一件容易的事。 However, you should be able to design a successful approach. 但是,您应该能够设计成功的方法。 This won't be the most error-safe method of accomplishing the task, though. 不过,这并不是完成任务的最错误安全的方法。 You're thinking right now of a HTTP-esque stateless protocol, which is manageable. 您现在正在考虑可管理的HTTP式无状态协议。 If the description below doesn't sound good enough, consider another protocol which can maintain a constant bi-directional connection (like an SSH tunnel). 如果以下说明听起来不够好,请考虑可以维持恒定双向连接的另一种协议(例如SSH隧道)。

You'd likely suffer data overhead, but that would generally be more than acceptable in order to save multiple calls. 您可能会遭受数据开销,但是为了节省多个调用,这通常会超出可接受范围。 To that end, I'd advise creating an XML interface. 为此,我建议创建一个XML接口。 On the receiving end, your XML would have an element with either a Base64 representation of the image, or possibly a gzipped CDATA implementation. 在接收端,您的XML将具有一个元素,该元素带有图像的Base64表示形式,或者可能带有gzip压缩的CDATA实现。 You don't have to stick to any XML standard, but if you do, the PHP XML Parser could help with some of the legwork. 您不必遵循任何XML标准,但是如果您这样做,PHP XML Parser可以帮助您完成一些工作。

So, to recap, in this model, the server end could receive a set of commands which do what you've called out: move the file into a processing folder, create a Base64 string of the file contents, craft the XMl package, and return it. 因此,总而言之,在此模型中,服务器端可能会收到一组命令,这些命令可以完成您所要执行的操作:将文件移至处理文件夹中,创建文件内容的Base64字符串,制作XMl包,以及把它返还。 The client will send a request, and process the response. 客户端将发送请求,并处理响应。 If the client detects an error, it could retry and the server can still grab the file data from the processing queue. 如果客户端检测到错误,则可以重试,并且服务器仍可以从处理队列中获取文件数据。

If error becomes an issue and an open socket isn't a good option (because the coding is difficult), you could also develop a delete-batching system, where you track the files in the processing folder and only delete them on request. 如果错误成为问题,并且开放套接字不是一个很好的选择(因为编码很困难),那么您还可以开发一个删除批处理系统,在其中跟踪处理文件夹中的文件,并仅在请求时将其删除。 But, you'd only make delete requests from the client every once in a while, and possibly not as a part of any particular page with a user experience, but from a cron. 但是,您只会偶尔每隔一段时间从客户端发出删除请求,并且可能不是cron,而是作为具有用户体验的任何特定页面的一部分。

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

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