douzhang5199 2014-10-29 03:54
浏览 15
已采纳

如何在PHP中解析一个简单的url响应?

I am trying to parse a simple response from the server and use its values.

I was able to get the required information as follows:

The actual response :

AccountID=0&Authenticated=1&ResponseCode=0&ResponseText=Success

What I need is separate values for :

  • AccountID
  • Authenticated
  • ResponseCode
  • ResponseText

My code so far :

$tempValue = explode("
", $response);

foreach($tempValue as $row => $data)          
{    
    //get row data
    $row_data = explode('&', $data);

    $row_internal = explode('=', $row_data);

    $info2[$row]['id']           = $row_internal[0];
    $info2[$row]['name']         = $row_internal[1];
    $info2[$row]['description']  = $row_internal[2];



    $info[$row]['id']           = $row_data[0];
    $info[$row]['name']         = $row_data[1];
    $info[$row]['description']  = $row_data[2];


    echo 'Account ID: ' . $info[$row]['id'] . '<br />';
    echo 'Authenticated: ' . $info[$row]['name'] . '<br />';
    echo 'Response Code: ' . $info[$row]['description'] . '<br />';
    echo '<br></br>';

    echo 'Account ID: ' . $info2[$row]['id'] . '<br />';
    echo 'Authenticated: ' . $info2[$row]['name'] . '<br />';
    echo 'Response Code: ' . $info2[$row]['description'] . '<br />';
}

Result for the above code :

Account ID: AccountID=0
Authenticated: Authenticated=1
Response Code: ResponseCode=0


Account ID: 
Authenticated: 
Response Code: 

What I needed was just the values for the fields like :

Account ID: 0
Authenticated: 1
Response Code: 0
  • 写回答

1条回答 默认 最新

  • doulu0266 2014-10-29 03:58
    关注

    If this is a query string response, then no need to explode, there is a better tool that handles this well.

    Just use parse_str().

    Simple one line response example:

    $response = 'AccountID=0&Authenticated=1&ResponseCode=0&ResponseText=Success';
    parse_str($response, $data);
    
    echo '<pre>';
    print_r($data);
    

    Or if the response looks like a multiline string like this, you could apply it like:

    $response = "AccountID=1&Authenticated=1&ResponseCode=0&ResponseText=Success
    AccountID=2&Authenticated=1&ResponseCode=0&ResponseText=Success
    AccountID=3&Authenticated=1&ResponseCode=0&ResponseText=Success
    ";
    
    $responses = explode("
    ", $response);
    foreach ($responses as $key => $value) {
    
        parse_str($value, $data);
    
        if(!empty($data)) {
            echo 'Account ID: '.$data['AccountID'] .'<br/>';
            echo 'Authenticated: '.$data['Authenticated'] .'<br/>';
            echo 'Response Code: '.$data['ResponseCode'] .'<br/>';
            echo 'Response Text: '.$data['ResponseText'] .'<br/>';
            echo '<br/>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示