How to get numeric value from cURL array?

I am trying to get a numeric value from a cURL response array. But failed to do so. I am using this code

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

      

It prints this Our new account ID: [{-10191}] on the page. Now I want to get 10191 from the array. I tried this but couldn't.

     $id = abs(filter_var(implode($returndata), FILTER_SANITIZE_NUMBER_INT));

      

How can I get the numeric value?

+3


source to share


1 answer


If you got $returndata

like string




Place this after 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);

      

+2


source







All Articles