I need some help in sending data in an array (POST) to a webservice (REST). I need some examples in how to use the function drupal_http_request()
.
Now, I'm using cURL with PHP but I need to replace it with drupal_http_request()
. And I see that it's easier to use in Drupal.
If my first page is the login (http://[ip]/users/sign_in) and I'm using "User:Pass" to enter to main page(http://[ip]/event). Then, I select the menu for view the data that I send from my form in Drupal (http://[ip]/event/mobile/management).
How to do this with drupal_http_request()
?
UPDATE:
function send_data_form_submit($form, &$form_state) {
$name = $form_state['values']['name'];
$email = $form_state['values']['email'];
$attendee = array(
'name' => urlencode($name),
'email' => urlencode($email)
);
$attendee = http_build_query($attendee, '', '&');
$url_login = 'http://[ip]/eventmobile/users/sign_in';
$url_target = 'http://[ip]/eventmobile/attendee_management/attendee';
$use = array(
'email' => '[email protected]',
'password' => '123456'
);
$user = http_build_query($use, '', '&');
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
$method = 'POST';
$result_1 = drupal_http_request(url($url_login, array(absolute => true)), $headers, $method, $user); //I get a http_code = 200. OK
$result_2 = drupal_http_request(url($url_target,array(absolute => true)), $headers, $method, $attendee); // I get a http_code = 500. Internal Server Error
}
UPDATE 2 (Using cURL):
$attendee = array(
'name' => urlencode($name),
'email' => urlencode($email)
);
$userpwd = "[email protected]:123456"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_target);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$getinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE); //I get http_code=200
if(preg_match('#Location: (.*)#', $result, $r))
$url_redirect = trim($r[1]); //The URL of LOGIN
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL, $url_redirect);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($ch); // Return string: Evaluating...
$getinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE); //I get http_code=200
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
$result = curl_exec($ch); // Return string: Evaluating...
$getinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE);// I get http_code=200
curl_close($ch);
curl
? because I never had any problems when switching fromcurl
todrupal_http_request
before...