The document discusses Drupal's API and REST services. It provides code examples for interacting with Drupal's API using cURL and Guzzle to make GET, POST, and authentication requests. It also covers configuring content types and models, permissions, and multilingual support in Drupal.
1 of 59
Download to read offline
More Related Content
Build a Restfull app using drupal
1. MAY 29 & 30, 2014
Barcelona
@alvar0hurtad0
Drupals API
26. Photo: Carlos Francisco 永艶単温
D
LAYOUT CLICK BY CLICK
DATA MODEL CLICK BY CLICK
DATABASE QUERYS CLICK BY CLICK
TRIGGERS AND ACTIONS
USER PERMISSIONS
WEB SERVICES CLICK BY CLICK
47. SHOW ME THE CODE
CURL
curl -H "Accept: application/hal+json" --request GET
http://drupal-8.localhost/node/1
48. SHOW ME THE CODE
CURL
curl -H "Accept: application/hal+json" --request GET
http://drupal-8.localhost/node/1
Guzzle
<?php
use GuzzleHttpClient;
$client = new Client('http://drupal-8.localhost');
// If in a Drupal environment use the HTTP client service.
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$request = $client->get('node/1');
$request->addHeader('Accept', 'application/hal+json');
$response = $request->send()->json();
print_r($response);
?>
49. SHOW ME THE CODE
CURL
curl -H "Accept: application/hal+json" --request GET
http://drupal-8.localhost/node/1
Guzzle
<?php
use GuzzleHttpClient;
$client = new Client('http://drupal-8.localhost');
// If in a Drupal environment use the HTTP client service.
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$request = $client->get('node/1');
$request->addHeader('Accept', 'application/hal+json');
$response = $request->send()->json();
print_r($response);
?>
CURL
curl --include --request POST
--user cleverFucker:secret
--header 'Content-type: application/hal+json' http://
drupal-8.localhost/entity/node
--data-binary '{"_links":{"type":{"href":"http://
drupal-8.localhost/rest/type/node/page"}}, "title":
[{"value":"APIdaysBNC"}]}'
50. SHOW ME THE CODE
CURL
curl -H "Accept: application/hal+json" --request GET
http://drupal-8.localhost/node/1
Guzzle
<?php
use GuzzleHttpClient;
$client = new Client('http://drupal-8.localhost');
// If in a Drupal environment use the HTTP client service.
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$request = $client->get('node/1');
$request->addHeader('Accept', 'application/hal+json');
$response = $request->send()->json();
print_r($response);
?>
CURL
curl --include --request POST
--user cleverFucker:secret
--header 'Content-type: application/hal+json' http://
drupal-8.localhost/entity/node
--data-binary '{"_links":{"type":{"href":"http://
drupal-8.localhost/rest/type/node/page"}}, "title":
[{"value":"APIdaysBNC"}]}'
Guzzle (I)
<?php
use GuzzleHttpClient;
$client = new Client('http://drupal-8.localhost');
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$node = array(
'_links' => array(
'type' => array(
'href' => 'http://drupal-8.localhost/rest/type/node/page'
)
),
'title' => array(0 => array('value' => 'New node title')),
);
51. SHOW ME THE CODE
CURL
curl -H "Accept: application/hal+json" --request GET
http://drupal-8.localhost/node/1
Guzzle
<?php
use GuzzleHttpClient;
$client = new Client('http://drupal-8.localhost');
// If in a Drupal environment use the HTTP client service.
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$request = $client->get('node/1');
$request->addHeader('Accept', 'application/hal+json');
$response = $request->send()->json();
print_r($response);
?>
CURL
curl --include --request POST
--user cleverFucker:secret
--header 'Content-type: application/hal+json' http://
drupal-8.localhost/entity/node
--data-binary '{"_links":{"type":{"href":"http://
drupal-8.localhost/rest/type/node/page"}}, "title":
[{"value":"APIdaysBNC"}]}'
Guzzle (I)
<?php
use GuzzleHttpClient;
$client = new Client('http://drupal-8.localhost');
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$node = array(
'_links' => array(
'type' => array(
'href' => 'http://drupal-8.localhost/rest/type/node/page'
)
),
'title' => array(0 => array('value' => 'New node title')),
);
Guzzle (II)
$data = json_encode($node);
$response = $client->post('entity/node', array(
'Content-type' => 'application/hal+json',
), $data)
// Username and password for HTTP Basic Authentication.
->setAuth('klausi', 'secret')
->send();
if ($response->getStatusCode() == 201) {
print 'Node creation successful!';
}
?>