# Getting Started with PHP

✋ CAUTION

This third-party integration guide might not be up-to-date with Strapi v4. Contributions (opens new window) are most welcome.

This integration guide is following the Quick Start Guide. We assume that you have fully completed its "Hands-on" path, and therefore can consume the API by browsing this url (opens new window).

If you haven't gone through the Quick Start Guide, the way you request a Strapi API with PHP (opens new window) remains the same except that you will not fetch the same content.

# Create a PHP file

Be sure to have PHP installed (opens new window) on your computer.

touch strapi.php
1

We will use cURL, a built-in PHP extension that allows us to receive and send information via the URL syntax.

# GET Request your collection type

Execute a GET request on the restaurant collection type in order to fetch all your restaurants.

Be sure that you activated the find permission for the restaurant collection type.

Example GET request

curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');
1

Running the PHP file on the browser will give you this response:

Example response

[{
  "id": 1,
  "name": "Biscotte Restaurant",
  "description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
  "created_at": "2021-03-31T11:37:16.964Z",
  "updated_at": "2021-03-31T11:37:16.975Z",
  "categories": [{
      "id": 1,
      "name": "French Food",
      "created_by": 1,
      "updated_by": 1,
      "created_at": "2021-03-31T11:36:23.164Z",
      "updated_at": "2021-03-31T11:36:23.172Z"
    },
    {
      "id": 2,
      "name": "Brunch ",
      "published_at": "2021-03-07T10:10:19.608Z",
      "created_at": "2021-03-07T10:10:14.477Z",
      "updated_at": "2021-03-07T10:10:19.649Z"
    }
  ]
}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Example

<?php
function getRestaurants(){
    $curl = curl_init(); //Initializes curl
    curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]); // Sets header information for authenticated requests

    $res = curl_exec($curl);
    curl_close($curl);
    print_r($res);
}

getRestaurants();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# POST Request your collection type

Execute a POST request on the restaurant collection type in order to create a restaurant.

Be sure that you activated the create permission for the restaurant collection type and the find permission for the category Collection type.

Example POST request

$restaurants = array(
  'name' => 'Calabar Kitchen',
  'description' => 'Omo, this is a place varieties soup with catfish🦈',
   'categories' => [2]
);

// Initializes a new cURL session
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Set the CURLOPT_POST for POST request
curl_setopt($curl, CURLOPT_POST, true);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Running the PHP file on the browser will give you this response:

Example response

[{
    "id": 2,
    "name": "Calabar Kitchen",
    "description": "Omo, this is a place that varieties of soup with catfish🦈",
    "published_at": "2021-03-14T11:23:13.251Z",
    "created_at": "2021-03-14T11:23:13.260Z",
    "updated_at": "2021-03-14T11:23:13.260Z",
    "categories": [{
        "id": 2,
        "name": "Brunch ",
        "published_at": "2021-03-14T09:22:58.909Z",
        "created_at": "2021-03-14T09:22:51.893Z",
        "updated_at": "2021-03-14T09:22:58.945Z"
        }
      ]
}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Example


<?php
function getRestaurants(){
    $curl = curl_init(); //Initializes curl
    curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]); // Sets header information for authenticated requests

    $res = curl_exec($curl);
    curl_close($curl);

    print_r($res);
}

function postRestaurant(){
    $restaurants = array(
        'name' => 'Calabar Kitchen',
        'description' => 'Omo, this is a place that varieties of soup with catfish🦈',
         'categories' => [2]
      );

      // Initializes a new cURL session
      $curl = curl_init();

      curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');

      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

      // Set the CURLOPT_POST for POST request
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($restaurants));

      curl_setopt($curl, CURLOPT_HTTPHEADER, [
          'Content-Type: application/json'
      ]);
      $res = curl_exec($curl);
      curl_close($curl);
      print_r($res);

}

postRestaurant();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# PUT Request your collection type

Execute a PUT request on the restaurant collection type in order to update the category of a restaurant.

Be sure that you activated the update permission for the restaurant collection type. PUT Request is slightly different as we need to target the particular entry we want update. We do this by first making a request to http://localhost:1337/api/restaurants/1 and then update what we want to update. In this example, we are going to update "Biscotte Restaurant" to "Femoni Kitchen".

Example PUT request

$restaurants = array(
    'name' => 'Femoni Kitchen'
  );

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants/1');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');

1
2
3
4
5
6
7
8
9

Example response

Running the PHP file on the browser will give you this response:

[{
  "id": 1,
  "name": "Femoni Kitchen",
  "description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
  "created_at": "2021-03-31T11:37:16.964Z",
  "updated_at": "2021-03-31T11:37:16.975Z",
  "categories": [{
      "id": 1,
      "name": "French Food",
      "created_by": 1,
      "updated_by": 1,
      "created_at": "2021-03-31T11:36:23.164Z",
      "updated_at": "2021-03-31T11:36:23.172Z"
    }
  ]
}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Example

<?php
function getRestaurants(){
    $curl = curl_init(); //Initializes curl
    curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]); // Sets header information for authenticated requests

    $res = curl_exec($curl);
    curl_close($curl);

    print_r($res);
}

function postRestaurant(){
    $restaurants = array(
        'name' => 'Calabar Kitchen',
        'description' => 'Omo, this is a place that varieties of soup with catfish🦈',
         'categories' => [2]
      );

      // Initializes a new cURL session
      $curl = curl_init();

      curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');

      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

      // Set the CURLOPT_POST for POST request
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($restaurants));
      curl_setopt($curl, CURLOPT_HTTPHEADER, [
          'Content-Type: application/json'
      ]);
      $res = curl_exec($curl);
      curl_close($curl);
      print_r($res);

}

function putRestaurant(){

$restaurants = array(
    'name' => 'Femoni Kitchen'
  );

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants/1');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($restaurants));
curl_setopt($curl, CURLOPT_HTTPHEADER, [
  'Content-Type: application/json'
]);

$res = curl_exec($curl);
curl_close($curl);

print_r($res);
}

putRestaurant();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

Running an authentication request (getting JWT):

Response


[{
    "jwt": "TOKEN",
    "user": {
        "confirmed": true,
        "blocked": false,
        "_id": "XXXXXXX",
        "email": "youremail@example.com",
        "username": "USERNAME",
        "provider": "local",
        "createdAt": "2021-03-22T08:03:04.717Z",
        "updatedAt": "2021-03-22T08:21:09.170Z",
        "__v": 0,
        "role": {
            "_id": "XXXXXXXXX",
            "name": "Authenticated",
            "description": "Default role given to authenticated user.",
            "type": "authenticated",
            "__v": 0,
            "id": "XXXXXXXXXX"
        },
        "id": "XXXXXXXX"
    }
}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# Example

<?php
$strapi_auth = [
    "identifier" => STRAPI_USERNAME,
    "password"  =>  STRAPI_USER_PWD
];




function strapi_auth_curl($url, $auth){
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url.'/auth/local',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>json_encode($auth),
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json'
        ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    return $response;
}
$login = strapi_auth_curl(STRAPI_URL, $strapi_auth);
$strapi_res = json_decode($login);

print_r($strapi_res);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

Running an authenticated POST request with JWT

Example response

[{
    "id": 2,
    "name": "Calabar Kitchen",
    "description": "Omo, this is a place that varieties of soup with catfish🦈",
    "published_at": "2021-03-14T11:23:13.251Z",
    "created_at": "2021-03-14T11:23:13.260Z",
    "updated_at": "2021-03-14T11:23:13.260Z",
    "categories": [{
        "id": 2,
        "name": "Brunch ",
        "published_at": "2021-03-14T09:22:58.909Z",
        "created_at": "2021-03-14T09:22:51.893Z",
        "updated_at": "2021-03-14T09:22:58.945Z"
        }
      ]
}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Example

<?php
$jwt = $strapi_res->jwt;
function postRestaurantWithAuth($jwt){
  $auth = array(
        'Authorization: Bearer '.$jwt,
        'Content-Type: application/json'
    );
    $restaurants = array(
        'name' => 'Calabar Kitchen',
        'description' => 'Omo, this is a place that varieties of soup with catfish🦈',
         'categories' => [2]
      );
      
      // Initializes a new cURL session
      $curl = curl_init();
      
      curl_setopt($curl, CURLOPT_URL, 'http://localhost:1337/api/restaurants');
      
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      
      // Set the CURLOPT_POST for POST request
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($restaurants));
      
      curl_setopt($curl, CURLOPT_HTTPHEADER, $auth);
      $res = curl_exec($curl);
      curl_close($curl);
      print_r($res);

}

postRestaurantWithAuth($jwt);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# Conclusion

Here is how to request your collection types in Strapi using PHP. When you create a collection type or a single type you will have a certain number of REST API endpoints available to interact with.

We just used the GET, POST and PUT methods here but you can get one entry, and delete an entry too. Learn more about API Endpoints.