Drupal 8+
$request = \Drupal::httpClient()->post('http://example.com', NULL, [
'param1' => 'value1',
'param2' => 'value2',
]);
try {
$response = $request->send();
}
catch (\Guzzle\Http\Exception\RequestException $exception) {
// ...
}
Drupal 7
$result = drupal_http_request('http://example.com', array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
'data' => drupal_http_build_query(array(
'param1' => 'value1',
'param2' => 'value2',
)),
));
Drupal 6
$result = drupal_http_request(
'http://example.com',
array('Content-Type' => 'application/x-www-form-urlencoded'),
'POST',
'param1=value1¶m2=value2'
);
Написанное актуально для
Drupal 6, Drupal 7, Drupal 8+
Комментарии
Вот это ОЧЕНЬ круто! То что надо. Спасибо.
Ай, спасиба, дарагой! Второй сутки по инету шарюсь, всё не то да не так, через редиректы всякие и $form['#method']. А, оказывается, вот как ещё можно!
А как отправлять файл с помощью drupal_http_request?
Нужно для загрузки видео на YouTube.
Пожалуйста... напишите аналогично - как получить POST запрос :'(
Присоединяюсь к Andrey Strelkov, напишите пожалуйста, как получить и обработать post-запрос
Вдруг если кому-то понадобиться сделать POST-запрос из формы.
Практикуется при работе с платежными системами.
function _test_payment_form() {
$form = array();
$form['user'] = array(
'#type' => 'hidden',
'#name-info' => 'user', // this is the field name that will become name attribute of the input tag. No need to use this if your $form['KEY'] is same as this one.
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Proceed to @server for the payment', array('@server' => 'Paypal')),
);
$form['#action'] = 'http://example-payment-server.com/example/2/3/4'; // Information will now be submitted to this URL other than Drupal.
$form['#method'] = 'POST'; // Just for clarification. This is the default value.
return $form;
}
Barcaman, а разве форма по умолчанию не отправляет POST?
Василий, так там так и написано
Fluffy, я имею ввиду, что и без параметра $form['#method'], форма будет отправлять POST
to Andrey Strelkov :
drupal_get_query_parameters()
Добавить комментарий