【问题标题】:How to get numeric value from cURL array?如何从 cURL 数组中获取数值?
【发布时间】:2015-07-11 07:01:09
【问题描述】:

我正在尝试从 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));

如何获取数值?

【问题讨论】:

    标签: php arrays curl


    【解决方案1】:

    如果你得到$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);
    

    【讨论】:

    • 是的,数据就像字符串。但它在我的情况下不起作用。我需要数值以供进一步使用,但我无法将其取出。
    • 你想要什么类型的变量?它为您提供 int 值。 @好友
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多