Skip to main content

PHP 入门

¥Getting Started with PHP

🧹 删除了集成指南

Strapi 文档团队专注于改进 Strapi 5 核心体验的文档。我们将在未来 6 个月内发布许多更改,请密切关注👀。

¥The Strapi Documentation team focuses on improving the documentation for Strapi 5's core experience. We will release many changes in the next 6 months, so please keep an eye out 👀.

因此,当前页面现在仅处于维护模式,可能未完全与 Strapi 5 保持同步,并且很快将从 docs.strapi.io 中删除并移至 Strapi 的 集成页面

¥As a result, the present page is now in maintenance mode only, might not be fully up-to-date with Strapi 5, and will soon be removed from docs.strapi.io and moved to Strapi's integrations page.

同时,如果你想帮助我们改进此页面,请随时 contribute 到文档的 GitHub 存储库。

¥In the meantime, if you want to help us improve this page, please feel free to contribute to the documentation's GitHub repository.

本集成指南遵循 快速入门指南。你应该能够通过浏览 URL http://localhost:1337/api/restaurants 使用该 API。

¥This integration guide follows the Quick Start Guide. You should be able to consume the API by browsing the URL http://localhost:1337/api/restaurants.

如果你尚未阅读快速入门指南,则使用 PHP 请求 Strapi API 的方式将保持不变,只是你不会获取相同的内容。

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

创建 PHP 文件

¥Create a PHP file

确保你的计算机上有 PHP 已安装

¥Be sure to have PHP installed on your computer.

touch strapi.php

我们将使用 cURL,这是一个内置的 PHP 扩展,它允许我们通过 URL 语法接收和发送信息。

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

GET 请求你的集合类型

¥GET Request your collection type

restaurant 集合类型执行 GET 请求以获取所有餐厅。

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

确保你激活了 restaurant 集合类型的 find 权限。

¥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');

Running the PHP file on the browser gives 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"
}
]
}]

示例

¥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();

POST 请求你的集合类型

¥POST Request your collection type

restaurant 集合类型执行 POST 请求以创建餐厅。

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

确保你激活了 restaurant 集合类型的 create 权限和 category 集合类型的 find 权限。

¥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);

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"
}
]
}]

示例

¥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();

PUT 请求你的集合类型

¥PUT Request your collection type

restaurant 集合类型执行 PUT 请求以更新餐厅的类别。

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

确保你激活了 restaurant 集合类型的 update 权限。PUT 请求略有不同,因为我们需要定位要更新的特定条目。为此,我们首先向 http://localhost:1337/api/restaurants/1 发出请求,然后更新我们想要更新的内容。在此示例中,我们要将 "比斯科特餐厅" 更新为 "费莫尼厨房"。

¥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');


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

Example 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"
}
]
}]

示例

¥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();

运行身份验证请求(获取 JWT):

¥Running an authentication request (getting JWT):

Example 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"
}
}]

示例

¥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);

使用 JWT 运行经过身份验证的 POST 请求:

¥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"
}
]
}]

示例

¥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);