Drupal 7 - Абнаўленне карыстальніка паслуг з дапамогай PHP Curl

I'm using Drupal 7 and the services module and I'm trying to update a user profile using PHP & Curl. Do I always have to login before sending a "PUT/update" ?

Гэта мой код да гэтага часу:

<?php


// REST Server URL
$request_url = 'http://mywebsite/end/user/login';

// User data
$user_data = array(
  'username' => 'user2',
  'password' => 'pass1',
);

// 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'));

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);

// REST Server URL
$request_url = 'http://mywebsite.com/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';

$user_data = array('current_pass' => 'pass1', 'pass' => 'pass2');

// Define cookie session
$cookie_session = $logged_user->session_name . '=' . $logged_user->sessid;

// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
    'Content-type: application/json'));//Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);//FALSE); //Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session");//use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);

// Emulate file.
$serialize_args = json_encode($user_data);
    $putData = fopen('php://temp', 'rw+');
    fwrite($putData, $serialize_args);
    fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, drupal_strlen($serialize_args));

$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

// Check if login was successful
$ret;
if ($http_code == 200) {
 //Convert json response as array
  $ret = json_decode($response);
}
else {
 //Get error msg
  $http_message = curl_error($curl);
  die($http_message);
}

print_r($ret);

curl_close($curl);

}



?>

Што я тут адсутнічае? Нічога не адбываецца ў маім профілі.

Любы адказ вітаецца!

4
Які метад аўтэнтыфікацыі вы выбіраеце для адпачынку службы сервера? Я прапаную Authetication Session. А вы праверылі дазволу на службовым модулі? Дазволеныя Ці вы Карыстальнік Ressource ў канфігурацыі службаў?
дададзена аўтар dba, крыніца
Які метад аўтэнтыфікацыі вы выбіраеце для адпачынку службы сервера? Я прапаную Authetication Session. А вы праверылі дазволу на службовым модулі? Дазволеныя Ці вы Карыстальнік Ressource ў канфігурацыі службаў?
дададзена аўтар dba, крыніца
Вы можаце паспрабаваць выкарыстоўваць CURLOPT_FAILONERROR ...
дададзена аўтар Anze, крыніца
Я выкарыстоўваю што :)
дададзена аўтар MDDY, крыніца
Але я атрымліваю тую ж памылку: 401 @Anze
дададзена аўтар MDDY, крыніца
Але я атрымліваю тую ж памылку: 401 @Anze
дададзена аўтар MDDY, крыніца

6 адказы

Спадзяюся, што гэта можа дапамагчы і.

$service_url = 'http://mywebsite/end/user/login';//.xml asks for xml data in response
    $post_data = array(
      'username' => 'user2',
      'password' => 'pass1',
    );

$post_data = http_build_query($post_data, '', '&');//Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //have curl_exec return a string
curl_setopt($curl, CURLOPT_POST, true);            //do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true);//output to command line
$response = curl_exec($curl);
curl_close($curl);

// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name .'='. $xml->sessid;
if(empty($xml->session_name) && empty($xml->sessid)){
    echo 'Wrong';exit;
}     

$service_url = 'http://mywebsite/end/user/token';//.xml asks for xml data in response

$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_POST, true);            //do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//POST this data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //have curl_exec return a string
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie");//use the previously saved session
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true);//output to command line
$csrf_token = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($csrf_token);
$csrf_token = $xml->token;
$csrf_header = 'X-CSRF-Token: ' . $csrf_token;

// REST Server URL
$request_url = 'http://mywebsite/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'testing');

// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
    'Content-type: application/json',$csrf_header));//Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
//curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data));//Set POST data
curl_setopt($curl, CURLOPT_HEADER, TRUE);//FALSE); //Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie");//use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
    $putData = fopen('php://temp', 'rw+');
    fwrite($putData, $serialize_args);
    fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($serialize_args));
$response = curl_exec($curl);    

curl_close($curl);
3
дададзена

Спадзяюся, што гэта можа дапамагчы і.

$service_url = 'http://mywebsite/end/user/login';//.xml asks for xml data in response
    $post_data = array(
      'username' => 'user2',
      'password' => 'pass1',
    );

$post_data = http_build_query($post_data, '', '&');//Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //have curl_exec return a string
curl_setopt($curl, CURLOPT_POST, true);            //do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true);//output to command line
$response = curl_exec($curl);
curl_close($curl);

// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name .'='. $xml->sessid;
if(empty($xml->session_name) && empty($xml->sessid)){
    echo 'Wrong';exit;
}     

$service_url = 'http://mywebsite/end/user/token';//.xml asks for xml data in response

$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_POST, true);            //do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//POST this data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //have curl_exec return a string
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie");//use the previously saved session
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true);//output to command line
$csrf_token = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($csrf_token);
$csrf_token = $xml->token;
$csrf_header = 'X-CSRF-Token: ' . $csrf_token;

// REST Server URL
$request_url = 'http://mywebsite/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'testing');

// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
    'Content-type: application/json',$csrf_header));//Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
//curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data));//Set POST data
curl_setopt($curl, CURLOPT_HEADER, TRUE);//FALSE); //Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie");//use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
    $putData = fopen('php://temp', 'rw+');
    fwrite($putData, $serialize_args);
    fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($serialize_args));
$response = curl_exec($curl);    

curl_close($curl);
3
дададзена

Спадзяюся, што гэта можа дапамагчы і.

$service_url = 'http://mywebsite/end/user/login';//.xml asks for xml data in response
    $post_data = array(
      'username' => 'user2',
      'password' => 'pass1',
    );

$post_data = http_build_query($post_data, '', '&');//Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //have curl_exec return a string
curl_setopt($curl, CURLOPT_POST, true);            //do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true);//output to command line
$response = curl_exec($curl);
curl_close($curl);

// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name .'='. $xml->sessid;
if(empty($xml->session_name) && empty($xml->sessid)){
    echo 'Wrong';exit;
}     

$service_url = 'http://mywebsite/end/user/token';//.xml asks for xml data in response

$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_POST, true);            //do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//POST this data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //have curl_exec return a string
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie");//use the previously saved session
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true);//output to command line
$csrf_token = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($csrf_token);
$csrf_token = $xml->token;
$csrf_header = 'X-CSRF-Token: ' . $csrf_token;

// REST Server URL
$request_url = 'http://mywebsite/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'testing');

// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
    'Content-type: application/json',$csrf_header));//Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
//curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data));//Set POST data
curl_setopt($curl, CURLOPT_HEADER, TRUE);//FALSE); //Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie");//use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
    $putData = fopen('php://temp', 'rw+');
    fwrite($putData, $serialize_args);
    fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($serialize_args));
$response = curl_exec($curl);    

curl_close($curl);
3
дададзена

You can achieve this by using "CURLOPT_COOKIEJAR" for writing and preserving cookies but you also need to set "CURLOPT_COOKIEFILE" for reading. More info can be found at http://php.net/manual/en/function.curl-setopt.php

define('COOKIE_FILE', "/tmp/sess" . time() . $user_data['username']);
curl_setopt ($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);
0
дададзена

You can achieve this by using "CURLOPT_COOKIEJAR" for writing and preserving cookies but you also need to set "CURLOPT_COOKIEFILE" for reading. More info can be found at http://php.net/manual/en/function.curl-setopt.php

define('COOKIE_FILE', "/tmp/sess" . time() . $user_data['username']);
curl_setopt ($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);
0
дададзена

You can achieve this by using "CURLOPT_COOKIEJAR" for writing and preserving cookies but you also need to set "CURLOPT_COOKIEFILE" for reading. More info can be found at http://php.net/manual/en/function.curl-setopt.php

define('COOKIE_FILE', "/tmp/sess" . time() . $user_data['username']);
curl_setopt ($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);
0
дададзена