Services 3.x REST server create update without storing NID on remote site

Events happening in the community are now at Drupal community events on www.drupal.org.
presm's picture

Im just at the begining of trying to understand services and rest, I have a simple php script that I can use to create resources from my remote site, this works fine as does updating resources when I have the NID to add into the script.

What I cant seem to get my head around is how I link create and updates from a remote site when the remote doesnt have or store the NID, I assumed that perhaps using CCk to store a Unique ID from the remote may be the answer but Im unsure how I would go about retrieving a NID from a REST call with a Unique CCK field.

The following code will create the entry in my test site no problems, if I append the http://mysite/course_rest_service/node/ with a node ID and change the curl op to CURLOPT_CUSTOMREQUEST 'PUT' this will update the node without any problems.

Im just not sure how to use services to find out if there is a field_unique_id(CCK Field with this value in that content type) get the NID for it to run the update operation.

* Server REST - user.login
*/

// REST Server URL
$request_url = 'http://mysite/course_rest_service/user/login';

// User data
$user_data = array(
'username' => 'name',
'password' => 'pass',
);
$user_data = http_build_query($user_data);

// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);

$response = curl_exec($curl);
$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);
}

// REST Server URL

$request_url = 'http://mysite/course_rest_service/node/';

// Node data
$node_data = array(
'field_unique_id[]' => array ('value' => '718000021955232'),
'title' => 'this should update 5537',
'type' => 'coursefeed',
'body' => '

losts my body

',
'taxonomy' => array(1=>array(19)),
'field_about_this_course[]' => array ('value' => '

information about a course

$node_data = http_build_query($node_data);
echo $node_data;

// 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')); // Accept JSON response
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $node_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, 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);

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

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

print_r($node);

Help, direction or better understanding of how this could be implemented easier would be greatly appreciated

Comments

Dirty way of getting NID

presm's picture

Code to return NID is by creating a View with exposed filter, using a XML feed output, The Exposed filter is based on a CCK field and returns the NID from this. Also someone asked how I update a node, this is dont by changing the curl op to PUT as below

// Get the NID from website with the template ID

    $nodeidraw="";

    if ($node_id!="")
    {
        $nids = new SimpleXMLElement("http://mywebsite/xmlid?field_course_id_value={$node_id}"  , null, true);

        if (isset($nids->node[0]->Nid))
        {
            $nodeidraw = $nids->node[0]->Nid;
        }
    }



    // REST Server URL
    $request_url = "http://mywebsite/course_rest_service/node/{$nodeidraw}";

    $node_data = http_build_query($uploadData);

    // 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')); // Accept JSON response
    if ($nodeidraw!="")
    {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); // Do an UPDATE PUT POST
        curl_setopt($curl, CURLOPT_POSTFIELDS, $node_data); // Set data for PUT
    } else {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); // Do a regular HTTP POST
        curl_setopt($curl, CURLOPT_POSTFIELDS, $node_data); // Set POST data
    }
    curl_setopt($curl, CURLOPT_HEADER, 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);

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

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

}

United Kingdom

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: