Jump to: navigation, search

PHP Manipulate the cURL return string and generate the array

From w3cyberlearnings

Contents

Purpose for the cURL request

After you send the request to the Restful or SOAP, or any URL by using the cURL, you will get the return response. In this demo, I will show you how to generate an array from the response through the Transaction Central post that I used the cURL.

Send Request to the Server

  • I send request to the server using cURL, and expect to get response back.
  • After get the response, I need to check the response information.
                   // collect information pass through this script
                   // and make it as an array.
                   $URL = 'https://soemthing.something.php';

                   $fields = array(
                        "MerchantID" => urlencode($merchantID),
                        "RegKey" => urlencode($regkey),
                        "AccountNo" => urlencode($c_number),
                        "CCMonth" => urlencode($c_month),
                        "CCYear" => urlencode($c_year),
                        "NameonAccount" => urlencode($c_name),
                        "CVV2" => urlencode($c_secret),
                        "RefID" => urlencode($referenceID),
                        "AVSADDR" => urlencode($address),
                        'Amount' => urlencode($total)
                    );

                    foreach ($fields as $k => $v) {
                        $string_field .= $k . '=' . $v . '&';
                    }
                    rtrim($string_field, '&');
            
                    $ch_post = curl_init($URL);
                    curl_setopt($ch_post, CURLOPT_POST, true);
                    curl_setopt($ch_post, CURLOPT_HEADER, false);
                    curl_setopt($ch_post, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch_post, CURLOPT_POSTFIELDS, $string_field);

                    $infor = curl_exec($ch_post); // return response content

                    curl_close($ch_post);
                    echo $infor;
                  

Original Out put

  • We need to manipulate the out put and store them to the array.
  <html>
	<body>	
	TransID=354611782&REFNO=16d9c870944de4079b:4432&Auth=08073B&AVSCode=N&CVV2ResponseMsg=M&Notes=Duplicate Order&User1=&User2=&User3=&User4=
	</body>
</html>

Manipulate the response content

  • The return string is not what we want, we need to store those return elements into array so that we can manipulate it.
                       
                    // include the array list of elements that we want to get rid off
                    $remove_a = array("<html>", "</html>", "<body>", "</body>");

                    // remove all the elements on the array list
                    $return_content = str_replace($remove_a, "", $infor);
                    
                    // remove all white spaces or spaces
                    $return_content = trim($return_content);
                    
                    // split the content string 
                    $my_aaa = explode("&", $return_content);

                    $a_return_a = array();
                    foreach ($my_aaa as $r) {
                        // split name and value pair and store in the array
                        $tmp = explode("=", $r);
                        $a_return_a[$tmp[0]] = $tmp[1];
                    }
                    print_r($a_return_a);

Out put

  • Now, you can check the return array for additional action such as to modify the order list table.
Array
(
    [TransID] => 354611782
    [REFNO] => 16d9c870944de4079b:4432
    [Auth] => 08073B
    [AVSCode] => N
    [CVV2ResponseMsg] => M
    [Notes] => Duplicate Order
    [User1] => 
    [User2] => 
    [User3] => 
    [User4] => 
)

Navigation
Web
SQL
MISC
References