Unpossible
Unpossible

Reputation: 10697

cUrl, PHP and a Accessing a Particular Header Type

I am writing a test harness to assist in testing a method in a PHP class that I have. Essentially, that method sends XML data as a POST request to the LinkedIn API, so for testing purposes, my test harness will act as the mock LinkedIn API endpoint, taking the posted data and returning various responses to see how the method handles them.

So, the method uses cUrl to send data like so:

$handle = curl_init()

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, 'http://localhost/target.php');
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);

$header[] = 'Content-Type: text/xml; charset=UTF-8';
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);

$data = '<?xml version="1.0" encoding="UTF-8"?>
         <activity locale="en_US">
           <content-type>linkedin-html</content-type>
           <body>Network update</body>
         </activity>';
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($handle);

Now, on my target.php script, I'd like to access the POSTed data... but when sending non key=value paired data, $_POST is always empty. If I omit the custom header and replace $data with $data = 'key=value', $_POST['key'] contains value. Keep in mind that this method works in terms of sending the XML data as above and the LinkedIn API responding.

So, any pointers on how to access POSTed data that is sent in non-key value pair format and possible a custom header type?

Upvotes: 1

Views: 369

Answers (2)

Unpossible
Unpossible

Reputation: 10697

Looks like the correct solution is to access the raw POST data via:

<?php $postdata = file_get_contents("php://input"); ?>

Per http://php.net/manual/en/reserved.variables.httprawpostdata.php

Upvotes: 1

Ēriks Daliba
Ēriks Daliba

Reputation: 718

It is called raw post data:

$xml = $HTTP_RAW_POST_DATA;

Upvotes: 0

Related Questions