dongqie2010 2017-10-04 10:34
浏览 231
已采纳

从curl响应字符串中获取特定变量

I am getting the response from curl as follows in a string:

Accepted=AVSAUTH:TEST:::829649376:N::U
ENTRYMETHOD=KEYED
historyid=829649376
MERCHANTORDERNUMBER=14700000186
orderid=646526156
PAYTYPE=MasterCard
recurid=0
refcode=829649376-TEST
result=1
Status=Accepted
transid=0

I want to get individual variables like status= Accepted so that i can able to store those in my database. Any help will be highly appreciable.

  • 写回答

3条回答 默认 最新

  • dqwr32867 2017-10-04 10:59
    关注

    preg_match is your friend:

    <?
    $result = "Accepted=AVSAUTH:TEST:::829649376:N::U
    ENTRYMETHOD=KEYED
    historyid=829649376
    MERCHANTORDERNUMBER=14700000186
    orderid=646526156
    PAYTYPE=MasterCard
    recurid=0
    refcode=829649376-TEST
    result=1
    Status=Accepted
    transid=0";
    
    preg_match_all("/^([^=]+)=(.*)$/m", $result, $regs, PREG_SET_ORDER);
    
    $data = [];
    foreach($regs as $reg) {
        $data[$reg[1]] = $reg[2];
    }
    
    print_r($data);
    ?>
    

    $data will be an hash Array:

    Array
    (
        [Accepted] => AVSAUTH:TEST:::829649376:N::U
        [ENTRYMETHOD] => KEYED
        [historyid] => 829649376
        [MERCHANTORDERNUMBER] => 14700000186
        [orderid] => 646526156
        [PAYTYPE] => MasterCard
        [recurid] => 0
        [refcode] => 829649376-TEST
        [result] => 1
        [Status] => Accepted
        [transid] => 0
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
  • ¥15 odbc代码新增sqlserver数据源
  • ¥15 求一个USB指纹识别的驱动
  • ¥15 wps中工作表与工作表之间怎么汇总信息?