1

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. 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.

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?

Also for multiple reasons ftp the file from the server to the client is not a good option.

Thank you

4

1 回答 1

0

这将不是一项微不足道的任务。但是,您应该能够设计一个成功的方法。不过,这不会是完成任务的最安全的方法。您现在正在考虑一种可管理的 HTTP 式无状态协议。如果下面的描述听起来不够好,请考虑另一种可以保持恒定双向连接的协议(如 SSH 隧道)。

您可能会遭受数据开销,但为了节省多个调用,这通常是可以接受的。为此,我建议创建一个 XML 接口。在接收端,您的 XML 将具有一个元素,该元素具有图像的 Base64 表示形式,或者可能是 gzip 压缩的 CDATA 实现。您不必遵守任何 XML 标准,但如果您这样做,PHP XML Parser可以帮助完成一些工作。

因此,回顾一下,在这个模型中,服务器端可以接收一组命令,这些命令执行您所调用的操作:将文件移动到处理文件夹中,创建文件内容的 Base64 字符串,制作 XMl 包,以及把它返还。客户端将发送请求,并处理响应。如果客户端检测到错误,它可以重试,服务器仍然可以从处理队列中获取文件数据。

如果错误成为问题并且打开的套接字不是一个好的选择(因为编码很困难),您还可以开发一个删除批处理系统,您可以在其中跟踪处理文件夹中的文件并仅在请求时删除它们。但是,您只会偶尔从客户端发出删除请求,并且可能不是作为具有用户体验的任何特定页面的一部分,而是来自 cron。

于 2010-11-19T00:15:07.463 回答