Tag Archives: PHP

PHP – Insert and element into a specific position of an Array

Originally we have an array as follow

$fruits = array(
  '0' => 'apple',
  '1' => 'banana',
  '2' => 'pear',
);

 

So i want to insert “melon” into the 2nd position which is after “apple”. We can do it by array_splice().
Continue reading PHP – Insert and element into a specific position of an Array

Drupal 8 – Create a node through RESTful Web Service @ 1

In the previous posts, we have gone through some examples about working with Drupal 8 RESTful web service.

 

We can also add node to Drupal through web service. But as i mentioned before, the RESTful feature is not fully completed so we can only create a node with a title. Here is a example web service client in PHP using Guzzle.
Continue reading Drupal 8 – Create a node through RESTful Web Service @ 1

Drupal 8 – Login in Guzzle Web Service Client @ 1

1. Get your PHP client ready by following my previous post.

 

2. Update your index.php as follow.

<?php
  require 'vendor/autoload.php';
  use Guzzle\Http\Client;
  use Guzzle\Plugin\Cookie\CookiePlugin;
  use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;

  $cookiePlugin = new CookiePlugin(new ArrayCookieJar());
  $client = new Client('http://drupal8.localhost.com');
  
  $client->addSubscriber($cookiePlugin); 
  $request = $client->post('user', null, array(
    'name' => '<username>',
    'pass' => '<password>',
    'form_id' => 'user_login_form',
  ));
  $request->addHeader('Accept', 'application/json');

  $response = $request->send()->json();

  print '<pre>';
  print_r($response);
  print '</pre>';
?>

Continue reading Drupal 8 – Login in Guzzle Web Service Client @ 1

Connect Drupal 8 RESTful Service with Guzzle PHP Web Service Client @ 1

Previously I have setup a a Drupal 8 which is RESTful ready.

Guzzle is a PHP HTTP client & framework for building RESTful web service clients. In this example, we would create a simple .php to fetch a node from Drupal 8.

1. Make sure you have the Composer installed. You can refer to my previous post.

Continue reading Connect Drupal 8 RESTful Service with Guzzle PHP Web Service Client @ 1

Composer – Manage your PHP dependencies

Mess up with the PHP dependencies in different environments? Composer is a PHP dependency manager where all the dependencies information are stored in the JSON file called composer.json. Similar to the pom.xml if you are using Maven in Java.

This example is done on a Windows machine. You could refer to Composer website if you are using Mac or Linux.

1. Download and install the Composer as stated in the Composer website.
Continue reading Composer – Manage your PHP dependencies

PHP – Sort a multidimensional array by specific key

Assume you have the following array in PHP.

$myArray = array(
  0 => array(
    'hashtag' => 'a7e87329b5eab8578f4f1098a152d6f4',
    'title'   => 'Flower',
    'order'   => 3
  ),
  1 => array(
    'hashtag' => 'b24ce0cd392a5b0b8dedc66c25213594',
    'title'   => 'Free',
    'order'   => 2
  ),
  2 => array(
    'hashtag' => 'e7d31fc0602fb2ede144d18cdffd816b',
    'title'   => 'Ready',
    'order'   => 1
  ),
);

Continue reading PHP – Sort a multidimensional array by specific key