12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-12-04 20:30:08
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
使用PHP进行GraphQL查询与获取数据的步骤如下:安装相关的PHP GraphQL库,推荐使用Webonyx GraphQL PHP库。可以通过Composer进行安装:composer require webonyx/graphql-php创建
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
使用PHP进行GraphQL查询与获取数据的步骤如下:
composer require webonyx/graphql-php
<?phpuse GraphQL\Type\Definition\ObjectType;use GraphQL\Type\Definition\Type;use GraphQL\Type\Schema;$queryType = new ObjectType(['name' => 'Query','fields' => ['hello' => ['type' => Type::string(),'resolve' => function () {return 'world';}]]]);$schema = new Schema(['query' => $queryType]);
<?phpuse GraphQL\GraphQL;use GraphQL\Type\Schema;// 获取请求中的GraphQL查询字符串$query = $_POST['query'];try {$result = GraphQL::executeQuery($schema, $query)->toArray();echo json_encode($result);} catch (Exception $e) {echo json_encode(['error' => ['message' => $e->getMessage()]]);}
<?php$query = '{hello}';$response = file_get_contents('http://localhost/graphql', false, stream_context_create(['http' => ['method' => 'POST','header' => 'Content-Type: application/json','content' => json_encode(['query' => $query])]]));$data = json_decode($response, true);print_r($data);
这样就可以使用PHP进行GraphQL查询与获取数据了。需要注意的是,GraphQL查询语句需要符合GraphQL的语法规范,可以通过GraphQL Playground等工具进行调试。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19