Created
April 11, 2014 15:47
-
-
Save drogers98/10479181 to your computer and use it in GitHub Desktop.
Curl D7 login junx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Retrieve CSRF token | |
$curl_get = curl_init(); | |
curl_setopt_array($curl_get, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => 'http://dev-oauth-test.gotpantheon.com/services/session/token', | |
)); | |
$csrf_token = curl_exec($curl_get); | |
curl_close($curl_get); | |
$csrf_header = 'X-CSRF-Token: ' . $csrf_token; | |
// REST Server URL | |
$request_url = 'http://dev-oauth-test.gotpantheon.com/myawesomejson/user/login'; | |
// User data | |
$user_data = array( | |
'username' => 'Test_user_2', | |
'password' => 'test2', | |
); | |
// cURL | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $request_url); | |
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $csrf_header)); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
$response = curl_exec($curl); | |
print $response; | |
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
// Check if login was successful | |
if ($http_code == 200) { | |
// Convert json response as array | |
$logged_user = json_decode($response); | |
} | |
else { | |
// Get error msg | |
$http_message = curl_error($curl); | |
die($http_message); | |
} | |
print_r($logged_user); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment