我试图从cURL响应数组中获取数值。但是我做不到。我正在使用这个代码
$urltopost = "http://www.xxxx.yyy.com/zzz.php";
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);
print_r($returndata);
它在页面上打印这个我们的新发票ID是:[{-10191}]。现在我想从数组中获取10191。我试过这个,但是失败了。
$id = abs(filter_var(implode($returndata), FILTER_SANITIZE_NUMBER_INT));
如何检索数值?
如果你得到了$returndata
就像字符串
把这个放在curl_exec后面:
<?php
$urltopost = "http://www.xxxx.yyy.com/zzz.php";
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);
//$returndata = 'Our new Invoice (ID) is: [{-10191}]';
preg_match_all("/{([^]]*)}/", $returndata, $matches);
$invoiceID = intval(ltrim($matches[1][0], '-'));
print_r($invoiceID);