arrays - PHP: Parsing a Post Response containing Multipart Form Data -



arrays - PHP: Parsing a Post Response containing Multipart Form Data -

i have been working api , used run cron job , create api phone call every 5 minutes. introduced feature similar paypal ipn posts variables 1 time order gets response.

i did print post variables , mailed have @ response be. code used.

$post_var = "results: " . print_r($_post, true); mail('email@mail.com', "post variables", $post_var);

and got in mail.

results: array ( [--------------------------918fc8da7040954f content-disposition:_form-data;_name] => "id" 1 --------------------------918fc8da7040954f content-disposition: form-data; name="txn" 1234567890 --------------------------918fc8da7040954f content-disposition: form-data; name="comment" test comment --------------------------918fc8da7040954f content-disposition: form-data; name="connectid" 1 --------------------------918fc8da7040954f content-disposition: form-data; name="connectname" test connect (nonexisting) --------------------------918fc8da7040954f content-disposition: form-data; name="status" unavailable --------------------------918fc8da7040954f content-disposition: form-data; name="callbackurl" http://www.example.com/ipn --------------------------918fc8da7040954f-- )

now need values of id i.e. 1, txn i.e. 1234567890 etc., never worked these kind of array. how proceed , response have got. curl response or multipart form info response?

please explain me if possible.

even though question 6 months old, i'll add together response here since i've had exact problem , couldn't find simple parser online.

assuming $response contains multi-part content:

// match boundary name taking first line content preg_match('/^(?<boundary>.+)$/m', $response, $matches); // explode response using match boundary $parts = explode($matches['boundary'], $response); // create empty array store our parsed values $form_data = array(); foreach ($parts $part) { // need parse multi-part content. first match 'name=' parameter, // skip double new-lines, match body , ignore terminating new-line. // using 's' flag enables .'s match new lines. $matched = preg_match('/name="?(?<key>\w+).*?\n\n(?<value>.*?)\n$/s', $part, $matches); // did match? place in our form values array if ($matched) { $form_data[$matches['key']] = $matches['value']; } } // check response... print_r($form_data);

i'm sure there's lot of caveats approach mileage may vary, satisfied need (parsing bitbucket snippet api response). comments/suggestions welcome.

php arrays curl multipartform-data

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -