如何RAW POST巨大的XML文件与curl - PHP [英] How to RAW POST huge XML file with curl - PHP

查看:182
本文介绍了如何RAW POST巨大的XML文件与curl - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是有办法做的

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

但是直接在PHP中?



CURLOPT_PUT / CURLOPT_UPLOAD 以及 file_get_contents 以及 exec



解决方案



我有一个类似的问题,试图从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 CURLOPT_PUT



<> <$ c $> p>您必须将 Content-Type:标头修改为服务器所期望的内容。



tcpdump,我可以确认请求方法是POST:

  POST / my_index / _bulk HTTP / 1.1 
Host: 127.0.0.1:9200
接受:* / *
Content-Type:application / json
Content-Length:167401
预期:100-继续
$ b b [... snip ...]



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


is there a way of doing

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

but directly in PHP?

CURLOPT_PUT/CURLOPT_UPLOAD as well as file_get_contents as well as exec

are not a solutions as it must be POST and the file is huge so must be streamed.

Any ideas?

解决方案

I had a similar problem trying to feed huge ingestion files to elasticsearch's bulk API from PHP, until I realized that the bulk API endpoint accepted PUT requests. Anyway, this code performs POST requests with huge files:

$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);

Here, $tmpFile is the full path of the file containing the request body.

NOTE: The important part is setting CURLOPT_CUSTOMREQUEST to 'POST' even though CURLOPT_PUT is set.

You'd have to adapt the Content-Type: header to whatever the server is expecting.

Using tcpdump, I could confirm the request method to be 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...]

I'm using packages libcurl3 (version 7.35.0-1ubuntu2.5) and php5-curl (version 5.5.9+dfsg-1ubuntu4.11) on Ubuntu 14.04.

这篇关于如何RAW POST巨大的XML文件与curl - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆