2017-04-21 40 views
0

我很想用Laravel/Guzzle从API获取文件。从API获取文件(Guzzle,Laravel,PHP)

$API_Conn = $factory->create(new Client(), MemoQWSAPI::FileManager()); 

$chunkSize = 500000; 

$file_start = $API_Conn->call('BeginChunkedFileDownload', [[ 
    'fileGuid' => $file_Guid, 
    'zip' => false, 
]]); 

$fileStream = fopen(storage_path('/app/MemoQWSAPI/test.xliff'), 'w'); 

$fileBytesLeft = $file_start->fileSize; 

while($fileBytesLeft > 0){ 
    $chunkBytes = $API_Conn->call('GetNextFileChunk', [[ 
     'sessionId' => $file_start->BeginChunkedFileDownloadResult, 
     'chunkSize' => $chunkSize, 
    ]]); 

    $write = fwrite($fileStream, $chunkBytes, $chunkSize); 

    $fileBytesLeft -= $chunkSize; 
} 

$res = $API_Conn->call('EndChunkedFileDownload', [[ 
    'sessionId' => $file_start->BeginChunkedFileDownloadResult, 
]]); 

fclose($fileStream); 

$API_Conn->call('DeleteFile', [[ // Export the file in to a Xliff File 
    'fileGuid' => $file_Guid, 
]]); 

我从API文件与此呼吁:

$chunkBytes = $API_Conn->call('GetNextFileChunk', [[ 
    'sessionId' => $file_start->BeginChunkedFileDownloadResult, 
    'chunkSize' => $chunkSize, 
]]); 

我用它来保存文件:

$fileStream = fopen(storage_path('/app/MemoQWSAPI/test.xliff'), 'w'); 

fwrite($fileStream, $chunkBytes, $chunkSize); 

fclose($fileStream); 

问题是我没有得到任何数据API。

我在做什么错了?

+1

如果我们不知道API,很难回答你的问题。 API是否返回任何错误?你的代码甚至不会检查来自API的错误消息。 – Matey

+0

有3个电话: 1.'BeginChunkedFileDownload' - >我得到了SessionId和fileSize。 2.'GetNextFileChunk' - >迭代 3.'EndChunkedFileDownload' - >关闭。 关于API Specs是在C#上编写的,我在PHP上复制了这些代码,但是从2.step与fwrite获取数据的方式并没有获取任何数据。 如果我执行这样的代码那样的fwrite给我一个错误: fwrite()期望参数2是字符串,给定的对象。 –

+0

如果你有'fwrite'错误,你应该调查'$ chunkBytes'是什么类型。应该在API规范或PHP类的文档中提及('$ API_Conn') – Matey

回答

0

谢谢大家,但最后我发现了错误。

这是因为API服务器自动创建名称为Test.xliff的文件,但在服务器上如何保存Test.xliff.dff89302a093s8402934a23948文件名+令牌。

无论如何谢谢你们所有人。