簡體   English   中英

如何使用curl來生成POST巨大的XML文件 - PHP

[英]How to RAW POST huge XML file with curl - PHP

有辦法嗎?

curl -X POST -H "Content-Type:application/xml" --data @myfile.xml http://example.com

但直接在PHP?

CURLOPT_PUT / CURLOPT_UPLOAD以及file_get_contents以及exec

不是解決方案,因為它必須是POST,文件很大,所以必須流式傳輸。

有任何想法嗎?

我有一個類似的問題,嘗試將大量的攝取文件從PHP提供給elasticsearch的批量API,直到我意識到批量API端點接受了PUT請求。 無論如何,這段代碼使用大文件執行POST請求:

$curl = curl_init();
curl_setopt( $curl, CURLOPT_PUT, 1 );
curl_setopt( $curl, CURLOPT_INFILESIZE, filesize($tmpFile) );
curl_setopt( $curl, CURLOPT_INFILE, ($in=fopen($tmpFile, 'r')) );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ] );
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($curl);
curl_close($curl);
fclose($in);

這里, $tmpFile是包含請求主體的文件的完整路徑。

注:重要的部分是設置CURLOPT_CUSTOMREQUEST'POST'即使CURLOPT_PUT設置。

您必須將Content-Type:標頭調整為服務器所期望的任何Content-Type:

使用tcpdump,我可以確認請求方法是POST:

POST /my_index/_bulk HTTP/1.1
Host: 127.0.0.1:9200
Accept: */*
Content-Type: application/json
Content-Length: 167401
Expect: 100-continue

[...snip...]

我在Ubuntu 14.04上使用包libcurl3(版本7.35.0-1ubuntu2.5)和php5-curl(版本5.5.9 + dfsg-1ubuntu4.11)。

Curl支持post,所以我相信你正在尋找這樣的東西: 使用Curl使用PHP發布或上傳文件

// URL on which we have to post data
$url = "http://localhost/tutorials/post_action.php";
// Any other field you might want to catch
$post_data['name'] = "khan";
// File you want to upload/post
$post_data['file'] = "@c:/logs.log";

// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);

// Just for debug: to see response
echo $response;

(代碼原始鏈接作者的所有功勞)。

至於“巨大”,你會想要更具體 - kb,mb,gb,tb? 其他問題將與PHP腳本保持活動多長時間而不是自動終止,腳本內存使用(可能需要以塊為單位處理而不是加載整個文件)等有關。

編輯:啊,對於RAW帖子我認為你將需要這個: 在PHP中使用Curl的原始POST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM