简体   繁体   English

如何在PHP中获得curl响应?

[英]How to get curl response in PHP?

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

Currently I am getting this: 目前,我得到这个:

<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>

I want to store error-description alone in a table. 我想将错误描述单独存储在表中。 How can I get error-description? 如何获得错误描述?

You can get error description using regex: 您可以使用正则表达式获取错误说明:

$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] : '';

Because it seems you have an XML response you could try an XML parser, but I think this is an easier solution. 因为您似乎有XML响应,所以可以尝试使用XML解析器,但是我认为这是一个更简单的解决方案。

Hope I helped. 希望我能帮上忙。 :) :)

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

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