如何在PHP中获得curl响应?

时间:2015-03-30 09:08:11

标签: php codeigniter curl

$ch = curl_init();

//Set cURL options
curl_setopt_array($ch, array(
  CURLOPT_HTTPHEADER => $header,
  CURLOPT_URL => $ls,
  CURLOPT_NOBODY => 1,
  CURLOPT_FAILONERROR => TRUE,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 
  CURLOPT_USERPWD => $user . ":" . $password, 
  CURLOPT_HTTPGET => TRUE //Set cURL to GET method
));

$data = curl_exec($ch);
print_r($data);

目前我收到了这个:

<smslist> <error> <smsclientid>0</smsclientid> <error-code>-10002</error-code> <error-description>Invalid Username Or Password</error-description> <error-action>1</error-action> </error> </smslist>

我想将错误描述单独存储在表中。我怎样才能得到错误描述?

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式获取错误说明:

$matches = array();
// we use ? because we want to stop at first </error-description>
// we use preg_match because we want only one error-description text
preg_match('/<error-description>(.+?)<\/error-description>/', $data, $matches);
$errorDescription = isset($matches[1]) ? $matches[1] : '';

因为您似乎有XML响应,所以可以尝试使用XML解析器,但我认为这是一个更简单的解决方案。

希望我能帮到你。 :)