解析cURL以获取PHP信息

时间:2012-04-26 10:16:12

标签: php curl

我有一个cURL回复:

<html version="-//W3C//DTD XHTML 2.0//EN" xml:lang="en" xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEAD>
<TITLE>Request Error</TITLE>
</HEAD>
<BODY>
<DIV id="content">
<P class="heading1"><B>Error Status Code:</B> 'BadRequest'</P>
<P><B>Details: </B>Customer email address is not unique - xxxxxxxxx@xxxxxx.com</P>
</DIV>
</BODY>
</html>

当我通过SimpleXMLElement将其转换为XML对象时,我无法访问P标签内的内容。有没有快速的方法来获得标头响应(400)或更好的cURL系统发送的信息。

我收到了结果:

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLCERT, SPEKTRIX_CERT);
curl_setopt($ch, CURLOPT_SSLKEY, SPEKTRIX_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'handleHeader');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'handleHeader');
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'w'));
$requestHeaders = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
$result = curl_exec($ch);

感谢您的任何见解

1 个答案:

答案 0 :(得分:2)

这个怎么样:

...prepare $ch...
$output = curl_exec($ch);
$info = curl_getinfo($ch);
echo $info['http_code']; // will output 400 for an invalid request