Jump to content

Parse incoming Post data into an array


Duderino7

Recommended Posts

I have an incoming http post that has multiple elements in one parameter that I need to parse as an array. I'm not a coder, but I can dabble and implement code. My programmer is on vacation.

 

I should also mention that in my index.php I have another similar piece of code that parses a json string from another posting source.

Here's what that code looks like:

if (isset($_REQUEST['data_json'])) {
$json_data = json_decode($_REQUEST['data_json'], true);
$_REQUEST = array_merge($_REQUEST, $json_data);
foreach ($_REQUEST as $key => $value) {
if (is_array($_REQUEST[$key]))
$_REQUEST[$key] = $_REQUEST[$key][0];
}
}

 

 

If you look below at [REQUEST_URI], that has the data I want parsed . But I have other similar posts from other sources that have the [REQUEST_URI] that I do not need parsed, so there would have to be a rule distinguishing this post type from others. The unique identifier is the [source_slug] at the top.

***********1433984376************
Array
(
[source_slug] => callrail
)
Array
(
[PATH] => /sbin:/usr/sbin:/bin:/usr/bin
[PP_CUSTOM_PHP_INI] => /home2/ezcrweb.com/etc/php.ini
[FCGI_ROLE] => RESPONDER
[REDIRECT_PP_CUSTOM_PHP_INI] => /home2/ezcrweb.com/etc/php.ini
[REDIRECT_STATUS] => 200
[HTTP_HOST] => ezcrweb.com
[HTTP_X_REAL_IP] => 199.182.122.43
[HTTP_X_FORWARDED_FOR] => 199.182.122.43
[HTTP_X_ACCEL_INTERNAL] => /internal-nginx-static-location
[HTTP_CONNECTION] => close
[CONTENT_TYPE] => application/json
[HTTP_X_NEWRELIC_ID] => VQMHWFJSGwIHVlJTBgc=
[HTTP_X_NEWRELIC_TRANSACTION] => PxRWUAdXC1ZSUlBbBVMPBwIFFB8EBw8RVU4aAwlZAAFWBw9QCFNRWgVQA0NKQQwGCVJRUgJTFTs=
[CONTENT_LENGTH] => 1061
[sERVER_SIGNATURE] => Apache Server at ezcrweb.com Port 80

[sERVER_SOFTWARE] => Apache
[sERVER_NAME] => ezcrweb.com
[sERVER_ADDR] => 10.182.162.130
[sERVER_PORT] => 80
[REMOTE_ADDR] => 199.182.122.43
[DOCUMENT_ROOT] => /home2/ezcrweb.com/httpdocs
[sERVER_ADMIN] => gary@ezcrfinancial.com
[sCRIPT_FILENAME] => /home2/ezcrweb.com/httpdocs/leads/index.php
[REMOTE_PORT] => 57665
[REDIRECT_QUERY_STRING] => source_slug=callrail
[REDIRECT_URL] => /leads/callrail
[GATEWAY_INTERFACE] => CGI/1.1
[sERVER_PROTOCOL] => HTTP/1.0
[REQUEST_METHOD] => POST
[QUERY_STRING] => source_slug=callrail
[REQUEST_URI] => /leads/callrail?callercity=Phoenix&callercountry=US&callername=Person+Joe&callernum=%2B16025540000&callerstate=AZ&callerzip=&callsource=keyword&datetime=2015-06-11+00%3A41%3A53&destinationnum=8019216388&first_call=true&gclid=CjwKEAjwwN-rBRD-oMzT6aO_wGwSJABwEIkJdO3j8lr7vjruFXpHqxxRzgMmVXPtdCvKPCq5tLImQBoC4bvw_wcB&id=314653359&ip=172.56.17.134&keywords=+get++out++car&landingpage=http%3A%2F%2Fbadautodebt.com%2Fget-car-back-az-simpson%2F%3Fkeyword%3D%252Bget%2520%252Bout%2520%252Bcar%26gclid%3DCjwKEAjwwN-rBRD-oMzT6aO_wGwSJABwEIkJdO3j8lr7vjruFXpHqxxRzgMmVXPtdCvKPCq5tLImQBoC4bvw_wcB&referrer=direct&referrermedium=CPC&trackingnum=%2B14804851256&transcription=&utm_campaign=&utm_content=&utm_medium=&utm_source=&utm_term=&utma=206296960.171304145.1433983274.1433983274.1433983274.1&utmb=206296960.1.10.1433983274&utmc=206296960&utmv=&utmz=206296960.1433983274.1.1.utmgclid%3DCjwKEAjwwN-rBRD-oMzT6aO_wGwSJABwEIkJdO3j8lr7vjruFXpHqxxRzgMmVXPtdCvKPCq5tLImQBoC4bvw_wcB%7Cutmccn%3D%28not+set%29%7Cutmcmd%3D%28not+set%29
[sCRIPT_NAME] => /leads/index.php
[php_SELF] => /leads/index.php
[REQUEST_TIME] => 1433984376
)

Link to comment
Share on other sites

The REQUEST_URI you are wanting is being populated via the $_SERVER superglobal.  You can get it directly by calling $_SERVER['REQUEST_URI'] instead of trying to find it in a multidimensional array.

As far as parsing it?  Who knows, I have no idea what you are wanting out of it.

I am going to guess that you want the uri query string parsed into an array.  If so, this will give you that, NOTING however, that if the value is empty, the key will not be in the array.  From your string given, this would be caller_zipcode, etc.
Array held in $parsed_uri

$pattern = '/(?<key>[a-zA-Z]+?)(?=\=[^&])\=(?<value>.[^&]+)/';
$string = $_SERVER['REQUEST_URI'];
preg_match_all($pattern,$string,$matches);
 
$count = count($matches[0]);
for($i = 0; $i < $count; $i++) {
$parsed_uri[$matches['key'][$i]] = $matches['value'][$i];
}
unset($matches);
echo '<pre>' . print_r($parsed_uri,true) . '</pre>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.