简体   繁体   English

PHP cURL设置编码?

[英]PHP cURL set encoding?

I use cURL to transfer data between two servers - both running Ubuntu 12.04, Lighttpd and PHP5.5 FastCGI. 我使用cURL在两个服务器之间传输数据-两者都运行Ubuntu 12.04,Lighttpd和PHP5.5 FastCGI。 I used to bzcompress the data - this is purely a legacy issue: I had found that bzcompress gives more efficient compression when writing out text data to files. 我曾经bzcompress数据-这纯粹是一个遗留问题:我发现bzcompress在将文本数据写到文件时可以提供更有效的压缩。 The data transferred tended to be quite small - typically of the order of under 512 bytes. 传输的数据往往很小-通常小于512字节。

However, today I ran into an issue when the data were somewhat longer - closer to 1 kB. 但是,今天我遇到了一个问题,即数据有些长-接近1 kB。 curl_exec duly returned true and no errors were reported. curl_exec正确返回true,并且未报告任何错误。 However, the data never arrived at their destination. 但是,数据永远不会到达目的地。 My original code was as follows 我的原始代码如下

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"{$cql}");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);

I suspected that there was an issue with encoding and, perhaps with my bzcompression. 我怀疑编码可能有问题,也许我的bzcompression也有问题。 So I replaced bzcompress with gzdeflate and altered my curl code to 所以我用gzdeflate替换了bzcompress并将curl代码更改为

curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:text/plain'));
curl_setopt($ch,CURLOPT_ENCODING,'');
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"{$cql}");

This works - no more vanishing data. 这有效-不再消失的数据。 However, the "solution" is purely the result of cobbling together an alternative based on some reading of the PHP documentation and some of the posts here - not a thing to be relied on for what is a small but mission critical part of what I am doing. 但是,“解决方案”纯粹是基于对PHP文档的阅读和此处的一些帖子,将一个替代方案汇总在一起的结果-对于我来说,这不是很小但对任务至关重要的部分,因此不值得依赖在做。

So the question - what is going on here? 那么问题来了-这是怎么回事? Why did the original code fail with longer data strings and why does the latter version work? 为什么原始代码在包含较长数据字符串的情况下会失败,为什么后一个版本会起作用? Will it always work or is there something else that is missing? 它会一直工作还是有其他遗漏的东西?

I would much appreciate any help and tips. 我将不胜感激任何帮助和提示。

Time to answer my own question. 是时候回答我自己的问题了。 The "solution" I have outlined above is little more than a red herring. 我上面概述的“解决方案”不过是红色鲱鱼。 It will not get you anywhere so don't bother trying it. 它不会带您到任何地方,所以请不要尝试。

The real issue here is best appreciated by noting that the server in question is Lighttpd. 通过注意所讨论的服务器是Lighttpd,可以最好地了解此处的实际问题。 Having researched matters a bit I have discovered that it has an unfortunate habit of sending out an HTTP 417 header when it gets a request with a http Expect: 100-continue header. 经过一些研究后,我发现它有一个不幸的习惯,即当它收到带有HTTP Expect:100-continue标头的请求时发送HTTP 417标头。

Either it appears to do this in an intermittent manner or else libcurl appends this header in an intermittent manner - perhaps when POST data length exceeds a certain threshold. 似乎是以间歇方式执行此操作的,或者libcurl是以间歇方式附加此标头的-也许是在POST数据长度超过某个阈值时。

In any case this was at the root of my problems. 无论如何,这是我问题的根源。 The right solution is to edit the lighttpd.conf file, add a 正确的解决方案是编辑lighttpd.conf文件,添加一个

server.reject-expect-100-with-417 = "disable"

line, and reload the server configuration 行,然后重新加载服务器配置

sudo service lighttpd force-reload

This prevents your own lighty installation from falling over when an Expect 100-Continue is encountered but does little if a libcurl request you make reaches a lighty (or another) server which throws up its hands in dismay when it sees that. 这样可以防止遇到“期望100-继续”时您自己的轻量级安装失败,但是如果您发出的libcurl请求到达轻量级(或其他)服务器,则服务器在看到轻量级(或其他)服务器时却举手投足,则几乎没有作用。 The only way to avoid that 避免这种情况的唯一方法

curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:  "));

This has cost me half a day of work. 这花了我半天的时间。 Hopefully, it will help someone else here. 希望它将对这里的其他人有所帮助。

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

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