RESTws using PHP-CURL to update or add data. Loging in and authentication

Although there is an article here about it http://blog.ampli.fi/creating-drupal-7-nodes-with-php-via-the-restws-ap…
I will copy mine code I used to test, just in case.

Things to watch out for is NOT to include JSON suffix, when using CURL

* Update: HTTP PUT //.
or HTTP PUT // (requires HTTP
Content-Type header set to the MIME type of the posted format)

and the hard limit when querying.

The hard limit of 100 resources per request ensures that the database and webserver
won't overload. The hard limit is defined by the system variable
restws_query_max_limit, which can be overridden if necessary.

Also, dont make a mistake and try to use PUT or POST on non entity endpoints. Like I did with

http://dev.local/commerce_product.json?sku=569

which is just a list of data and can't be used other then to read(GET).

$site = "dev.local";
$user = "restws_236216";
$pass = "restws";
$crl = curl_init();
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_USERAGENT, 'PHP script');
curl_setopt($crl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt($crl, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');

// Login
curl_setopt($crl, CURLOPT_URL,"http://" . $site . "/user/login");
curl_setopt($crl, CURLOPT_POST, TRUE);
curl_setopt($crl, CURLOPT_POSTFIELDS, "name=" . $user . "&pass=" . $pass . "&form_id=user_login");
curl_setopt($crl, CURLOPT_COOKIE, session_name() . '=' . session_id());
$ret = new stdClass;
$ret->response = curl_exec($crl);
$ret->error = curl_error($crl);
$ret->info = curl_getinfo($crl);

// Get RESTWS token.
curl_setopt($crl, CURLOPT_HTTPGET, TRUE);
curl_setopt($crl, CURLOPT_URL, 'http://' . $site . '/restws/session/token');
$ret = new stdClass;
$ret->response = curl_exec($crl);
$ret->error = curl_error($crl);
$ret->info = curl_getinfo($crl);
$token = $ret->response;

// Do the API call to create the node. Places tagged with "UPDATING" relate to updating existing nodes instead of creating new ones.
date_default_timezone_set("Europe/Helsinki");
$data = json_encode(array(
//'field_section_1_title' => 'range_page', // UPDATING: Don't try to set the "type" for existing nodes.
    'commerce_price_aud' => array (
      'amount' => 320,
      'currency_code' => 'AUD',
    ),
'title' => 'It is a new article, created by ' . $user . ' at ' . date('c'),

));

// UPDATING: Use PUT instead of POST when updating a node
//curl_setopt($crl, CURLOPT_POST, TRUE);
//curl_setopt($crl, CURLOPT_POST, FALSE);
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, "PUT");

// UPDATING: Add the node id when updating a node
//curl_setopt($crl, CURLOPT_URL, 'http://' . $site . '/node/1');
curl_setopt($crl, CURLOPT_URL, 'http://' . $site . '/commerce_product/1092'); 

curl_setopt($crl, CURLOPT_HTTPGET, FALSE);
curl_setopt($crl, CURLOPT_POSTFIELDS, $data);
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-CSRF-Token: ' . $token));
$ret = new stdClass;
$ret->response = curl_exec($crl);
$ret->error = curl_error($crl);
$ret->info = curl_getinfo($crl);

var_dump($data);
curl_close ($crl);
unset($crl);