Almost any working PHP programmer has faced having to use CURL to send raw HTTP requests, whether it's for credit card payment processing, nefarious screen scraping, or something in-between.
几乎任何正在运行的PHP程序员都必须使用CURL来发送原始HTTP请求,无论是用于信用卡支付处理,恶意屏幕抓取还是介于两者之间。
Almost any forum where PHP programmers congregate has a large number of people who can't get the cURL functions to do what they want.
几乎所有PHP程序员聚集的论坛都有很多人无法获得cURL功能来做他们想做的事情。
When cURL isn't working for you, what troubleshooting techniques do you use to figure out why it isn't working? What weird gotchas with PHP's curl implementation have you run into? If someone asks a "HALP MY CURL IZ BROKEN" question on a forum, what are the steps you take to figure out why their request isn't working?
当cURL不适合您时,您使用什么故障排除技术来找出它无法正常工作的原因? PHP的卷曲实现有什么奇怪的问题你遇到了什么?如果有人在论坛上询问“HALP MY CURL IZ BROKEN”问题,您采取了哪些步骤来弄清楚他们的请求无效的原因?
2 个解决方案
#1
I find the CURLINFO_HEADER_OUT option to be very useful.
我发现CURLINFO_HEADER_OUT选项非常有用。
<?php $curl = curl_init('http://www.php.net'); curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'dbg_curl_data'); curl_setopt($curl, CURLOPT_WRITEFUNCTION, 'dbg_curl_data'); curl_setopt($curl, CURLINFO_HEADER_OUT, true); curl_exec($curl); echo '<fieldset><legend>request headers</legend> <pre>', htmlspecialchars(curl_getinfo($curl, CURLINFO_HEADER_OUT)), '</pre> </fieldset>'; echo '<fieldset><legend>response</legend> <pre>', htmlspecialchars(dbg_curl_data(null)), '</pre> </fieldset>'; function dbg_curl_data($curl, $data=null) { static $buffer = ''; if ( is_null($curl) ) { $r = $buffer; $buffer = ''; return $r; } else { $buffer .= $data; return strlen($data); } }
#2
Actually, I never use CURL (in php). The PHP stream api is much neater, and can be used to POST data as well. Wez Furlong has an article about this.
实际上,我从不使用CURL(在php中)。 PHP流API非常简洁,也可以用于POST数据。 Wez Furlong有一篇关于此的文章。
If I were to use it? I'd start with turning on all messages (setting error reporting to E_ALL). If I find that PHP doesn't tell me what I need in the error messages, I'd probably use a proxy approach to see what's actually going on. Changing the target url to a local php page containing something like
如果我要用它?我首先打开所有消息(将错误报告设置为E_ALL)。如果我发现PHP没有告诉我错误消息中需要什么,我可能会使用代理方法来查看实际发生的情况。将目标URL更改为包含类似内容的本地php页面
<?php
var_dump($_POST);
var_dump($_GET);
var_dump($_SERVER);
is one way. Another way is to use a utility like netcat to listen on port 80 and send the request there:
是一种方式。另一种方法是使用像netcat这样的实用程序来侦听端口80并在那里发送请求:
netcat -l -p 80
This won't return anything to curl, but it will allow you to see exactly what is sent the server, which might be enough to diagnose the problem.
这不会返回任何卷曲,但它将允许您确切地查看服务器发送的内容,这可能足以诊断问题。
You can also retrieve the headers from PHP using the apache_request_headers() function. In most cases I prefer the netcat approach, though, since it guarantees that I see the unmodified truth, and also display the raw post data.
您还可以使用apache_request_headers()函数从PHP检索标头。在大多数情况下,我更喜欢netcat方法,因为它保证我看到未经修改的事实,并且还显示原始的帖子数据。
#1
I find the CURLINFO_HEADER_OUT option to be very useful.
我发现CURLINFO_HEADER_OUT选项非常有用。
<?php $curl = curl_init('http://www.php.net'); curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'dbg_curl_data'); curl_setopt($curl, CURLOPT_WRITEFUNCTION, 'dbg_curl_data'); curl_setopt($curl, CURLINFO_HEADER_OUT, true); curl_exec($curl); echo '<fieldset><legend>request headers</legend> <pre>', htmlspecialchars(curl_getinfo($curl, CURLINFO_HEADER_OUT)), '</pre> </fieldset>'; echo '<fieldset><legend>response</legend> <pre>', htmlspecialchars(dbg_curl_data(null)), '</pre> </fieldset>'; function dbg_curl_data($curl, $data=null) { static $buffer = ''; if ( is_null($curl) ) { $r = $buffer; $buffer = ''; return $r; } else { $buffer .= $data; return strlen($data); } }
#2
Actually, I never use CURL (in php). The PHP stream api is much neater, and can be used to POST data as well. Wez Furlong has an article about this.
实际上,我从不使用CURL(在php中)。 PHP流API非常简洁,也可以用于POST数据。 Wez Furlong有一篇关于此的文章。
If I were to use it? I'd start with turning on all messages (setting error reporting to E_ALL). If I find that PHP doesn't tell me what I need in the error messages, I'd probably use a proxy approach to see what's actually going on. Changing the target url to a local php page containing something like
如果我要用它?我首先打开所有消息(将错误报告设置为E_ALL)。如果我发现PHP没有告诉我错误消息中需要什么,我可能会使用代理方法来查看实际发生的情况。将目标URL更改为包含类似内容的本地php页面
<?php
var_dump($_POST);
var_dump($_GET);
var_dump($_SERVER);
is one way. Another way is to use a utility like netcat to listen on port 80 and send the request there:
是一种方式。另一种方法是使用像netcat这样的实用程序来侦听端口80并在那里发送请求:
netcat -l -p 80
This won't return anything to curl, but it will allow you to see exactly what is sent the server, which might be enough to diagnose the problem.
这不会返回任何卷曲,但它将允许您确切地查看服务器发送的内容,这可能足以诊断问题。
You can also retrieve the headers from PHP using the apache_request_headers() function. In most cases I prefer the netcat approach, though, since it guarantees that I see the unmodified truth, and also display the raw post data.
您还可以使用apache_request_headers()函数从PHP检索标头。在大多数情况下,我更喜欢netcat方法,因为它保证我看到未经修改的事实,并且还显示原始的帖子数据。