3

I'm trying to fetch numeric value from the cURL response array. But Unable to do that. I'm 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);

Its prints this Our new Invoice ID is: [{-10191}] on the page. Now I want to fetch 10191 from the array. I tried this one but failed.

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

How can i retreive the numeric value?

1 Answer 1

1

If You got the $returndata is like string


Put 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);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes the data is like string. But it didn't work in my case. I need the numeric value for further use but i couldn't get it out.
What type of variable you want? it gives you int value. @buddy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.