如何在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

我试图从服务器解析一个简单的响应并使用它的值。 p>

我能够获得如下所需的信息: p>

实际响应: strong> p>

   AccountID = 0&amp; Authenticated = 1&amp; ResponseCode = 0&amp; ResponseText = Success 
  code>  pre> 
 
 

我需要的是单独的值: p>

  • AccountID strong> li>
  • 经过身份验证 strong> li>
  • ResponseCode strong> li>
  • ResponseText strong> li> ul>

    到目前为止我的代码: strong> p>

      $ tempValue = explode(“
    ”,$ response); 
     
    foreach($ tempValue as $ row =&gt; $ data)
     {
     //获取行数据
      $ row_data = explode('&amp;',$ 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']。  '&lt; br /&gt;'; 
     echo'Tatedated:'。  $ info [$ row] ['name']。  '&lt; br /&gt;'; 
     echo'Response Code:'。  $ info [$ row] ['description']。  '&lt; br /&gt;'; 
     echo'&lt; br&gt;&lt; / br&gt;'; 
     
     echo'帐户ID:'。  $ info2 [$ row] ['id']。  '&lt; br /&gt;'; 
     echo'Tatedated:'。  $ info2 [$ row] ['name']。  '&lt; br /&gt;'; 
     echo'Response Code:'。  $ info2 [$ row] ['description']。  '&lt; br /&gt;'; 
    } 
      code>  pre> 
     
     

    以上代码的结果: strong> p>

     帐户ID:AccountID = 0 
    Authenticated:Authenticated = 1 
    Response代码:ResponseCode = 0 
     
     
    帐户ID:
    Authenticated:
    Response代码:
      code>  pre>  
     
     

    我需要的只是以下字段的值: p>

     帐户ID:0 
    Authenticated:1 
    Response代码:0 
      代码>  pre> 
      div>

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/>';
    }
}