简体   繁体   中英

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.

Hope I helped. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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