1

I have two websites use Drupal.

In Websites ONE: I have code

$data = array(
  'key1' => 'value1',
  'key2' => 'value2',
);
$request = array(
  'headers' => array(
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
  ),
  'data' => drupal_json_encode($data),
  'method' => 'POST',
  'max_redirects' => 3,
  // We set a lower timeout since we don't want the user waiting for the page too long
  'timeout' => 30,
);

$url = 'http://examplesitetwo.com/return-data';
$response = drupal_http_request($url, $request);

In Website TWO: i have code

function my_module_menu() {
  $items = array();

  $items['return-data'] = array(
    'page callback' => 'return_data_pcb',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function return_data_pcb() {
  // How to get post data form drupal_http_request in website one;
}

How to get post data form drupal_http_request() in website one? Thank you!

2

Sorry. It's not answer. But i try debug in function

function return_data_pcb() {
  watchdog("debug", '<pre>' . print_r( $_POST, true) . '</pre>'); 
  // But $_POST is empty array. It's show in admin/reports/dblog, when in website one // call function drupal_http_request.
}

I don't know my problem by when call drupl_http_request data post doesn't not send. Or problem with hook_menu in website two. Please help!

1

So, as far as I know Drupal handles post data the same way other php applications do with the $_POST variable. I just did something similar against an external server and it was helpful to print_r every step of building your request and step through to make sure everything is working the way you think. If your post variable is empty than you're probably not sending it correctly. I would try something like this for your 2nd site's page callback:

function return_data_pcb() {
  dpm($_POST);
}
1

To receive RAW post data in PHP, you can use the php://input stream like so:

$content = file_get_contents("php://input");

Then you can decode JSON

$decoded = json_decode($content, true);

So your full function can be something like this:

function return_data_pcb() {
  // Make sure that it is a POST request.
  if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
    throw new Exception('Request method must be POST!');
  }

  // Make sure that the content type of the POST request has been set to application/json.
  $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
  if (strcasecmp($contentType, 'application/json') != 0) {
    throw new Exception('Content type must be: application/json');
  }

  // Receive the RAW post data.
  $content = trim(file_get_contents("php://input"));

  // Attempt to decode the incoming RAW post data from JSON.
  $decoded = json_decode($content, TRUE);

  // If json_decode failed, the JSON is invalid.
  if (!is_array($decoded)) {
    throw new Exception('Received content contained invalid JSON!');
  }

  // Process the JSON.
}
0

What is your drupal version? I think this will be different between version 7 and version 8 as symphony provides helpers for the standard super globals I think. I'm still not up to speed on Drupal 8 yet.

You can always access the post data through the $_POST super global. Drupal is still a php engine.

Check out this other drupal answers case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.