Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some arrays I need to send back to the server but the data is large so I JSON encoded it resulting in something like this

{"help me":[{"href":"http:\/\/codeproject.com","key1":"20%","key2":"20%","key3":"65%}]}


Where there are about 40 arrays with their own hrefs and each of these arrays have over a hundred keys. I intend to identify this payload on the other script using something like $_POST['help me'] (gateway to dozens of arrays).

What I have tried:

I've tried using guzzle (which internally uses curl) and file get contents but neither can transport the data back.

$postClient = new GuzzleClient();

	$response = $postClient->request('POST', 'http://localhost/c.php', [
	    'json' => json_decode($data, true)
	]);

and
$postClient = new GuzzleClient();

	$response = $postClient->request('POST', 'http://localhost/c.php', [
	    'body' => $data,
        'Content-Type' => 'application/json'
	]);

and also
$opts = array('http' => array('method' => 'POST', 'header'  => 'Content-Type: application/json', 'content' => json_decode($data, true)['fixtures']));


$context = stream_context_create($opts);

$result = file_get_contents('http://localhost/c.php', false, $context);

But when I inspect the response header using $http_response_header, I find it was sent using content type text/html and the c.php which contains
var_dump($_POST);


Says the array is empty, when there should be an array there named 'help me'. So it seems to get to that script but without the transport parameters. But in reality, it sees those parameters because when I var_dump the $_SERVER superglobal instead of $_POST, it shows me all those parameters passed in the options. All except the request body or payload. The 'QUERY_STRING' key is empty so I tried sending in a URL encoded string instead using http_build_query but that function only works with flat arrays. I cannot even fathom transforming such an array into a query. I've also tried countless snippets of code online that promise the smooth delivery of the arrays but none works
Posted
Updated 14-Jul-17 3:00am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900