簡體   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);

目前,我得到這個:

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

我想將錯誤描述單獨存儲在表中。 如何獲得錯誤描述?

您可以使用正則表達式獲取錯誤說明:

$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解析器,但是我認為這是一個更簡單的解決方案。

希望我能幫上忙。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM