Webonyx/Graphql-php 入门:如何在 cURL 中获取 API 的响应,而没有来自 API 实现的回显?
Getting started in Webonyx/Graphql-php: How get the response of the API in cURL without echoes from the API implementation?
我是 GraphQL 的新手,想构建一个简单的 API 来开始。阅读文档并尝试示例后,API 终于可以正常工作了……但是 !!!
API 的 php 实现以“回声”结束(这对 Graphiql 客户端来说工作正常!),但是当我尝试在 cURL 中获取响应时,出现此回声在我的页面源中...
拜托各位,如何避免这种回显并在 cURL 中得到结果?我求助于巨大的集体智慧来获得一些帮助。
以下是我使用的资源:
- Composer(很明显!)
- Webonyx/GraphQl-php(php 的 graphQl)
- Illuminate/Database(为了使用mySql)
- GraphiQL extension for Chrome(测试 Api)
这是实现webonyx/graphql-php:
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
require('types.php');
require('querys.php');
require('mutations.php');
$schema = new Schema([
'description' => 'Available querys and mutations',
'query' => $rootQuery,
'mutation' => $rootMutation
]);
try
{
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$result = GraphQL::executeQuery($schema, $query);
$output = $result->toArray();
}
catch (\Exception $e){
$output = [
'error' => [
'message' => $e->getMessage()
]
];
}
header('Content-Type: application/json');
echo json_encode($output); //<-- This echo appear when i try to get the response in cURL
这里是 GraphiQL 中的查询:
mutation{
addUser(name:"user name",email:"user email",password:"some pass"){
id
}
}
这是 GraphQL 中的结果(一切正常!):
{
"data": {
"addUser": {
"id": 97
}
}
}
这里使用 cURL 中的函数来获取响应:
function addUser($name,$email,$password){
$query = <<<'JSON'
mutation{
addUser(name:"*name", email:"*email", password:"*password"){
id
name
}}
JSON;
$trans = array(
"*name" => $name,
"*email" => $email,
"*password" => $password
);
$query = strtr($query, $trans);
$variables = "";
$json = json_encode(['query' => $query, 'variables' => $variables]);
$chObj = curl_init();
curl_setopt($chObj, CURLOPT_URL, $this->endpoint);
curl_setopt($chObj, CURLOPT_RETURNTRANSFER, false);
curl_setopt($chObj, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($chObj, CURLOPT_VERBOSE, true);
curl_setopt($chObj, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($chObj);
return $result;
}
这是我使用函数时页面中的源代码:
{"data":{"addUser":{"id":98,"name":"user name"}}}[1]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.87.0">
<title>Dashboard Template · Bootstrap v5.1</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/dashboard/">
字符串:{"data":{"addUser":{"id":98,"name":"用户名"}}}来自API 实现 echo json_encode($output);
我尝试使用 get_file_content 代替 cURL 但无法取得好的效果,请提供任何帮助!
Set CURLOPT_RETURNTRANSFER
to true to return the transfer as a
string of the return value of curl_exec() instead of outputting it
directly.
curl_setopt($chObj, CURLOPT_RETURNTRANSFER, true);
我是 GraphQL 的新手,想构建一个简单的 API 来开始。阅读文档并尝试示例后,API 终于可以正常工作了……但是 !!!
API 的 php 实现以“回声”结束(这对 Graphiql 客户端来说工作正常!),但是当我尝试在 cURL 中获取响应时,出现此回声在我的页面源中...
拜托各位,如何避免这种回显并在 cURL 中得到结果?我求助于巨大的集体智慧来获得一些帮助。
以下是我使用的资源:
- Composer(很明显!)
- Webonyx/GraphQl-php(php 的 graphQl)
- Illuminate/Database(为了使用mySql)
- GraphiQL extension for Chrome(测试 Api)
这是实现webonyx/graphql-php:
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
require('types.php');
require('querys.php');
require('mutations.php');
$schema = new Schema([
'description' => 'Available querys and mutations',
'query' => $rootQuery,
'mutation' => $rootMutation
]);
try
{
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$result = GraphQL::executeQuery($schema, $query);
$output = $result->toArray();
}
catch (\Exception $e){
$output = [
'error' => [
'message' => $e->getMessage()
]
];
}
header('Content-Type: application/json');
echo json_encode($output); //<-- This echo appear when i try to get the response in cURL
这里是 GraphiQL 中的查询:
mutation{
addUser(name:"user name",email:"user email",password:"some pass"){
id
}
}
这是 GraphQL 中的结果(一切正常!):
{
"data": {
"addUser": {
"id": 97
}
}
}
这里使用 cURL 中的函数来获取响应:
function addUser($name,$email,$password){
$query = <<<'JSON'
mutation{
addUser(name:"*name", email:"*email", password:"*password"){
id
name
}}
JSON;
$trans = array(
"*name" => $name,
"*email" => $email,
"*password" => $password
);
$query = strtr($query, $trans);
$variables = "";
$json = json_encode(['query' => $query, 'variables' => $variables]);
$chObj = curl_init();
curl_setopt($chObj, CURLOPT_URL, $this->endpoint);
curl_setopt($chObj, CURLOPT_RETURNTRANSFER, false);
curl_setopt($chObj, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($chObj, CURLOPT_VERBOSE, true);
curl_setopt($chObj, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($chObj);
return $result;
}
这是我使用函数时页面中的源代码:
{"data":{"addUser":{"id":98,"name":"user name"}}}[1]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.87.0">
<title>Dashboard Template · Bootstrap v5.1</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/dashboard/">
字符串:{"data":{"addUser":{"id":98,"name":"用户名"}}}来自API 实现 echo json_encode($output);
我尝试使用 get_file_content 代替 cURL 但无法取得好的效果,请提供任何帮助!
Set
CURLOPT_RETURNTRANSFER
to true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
curl_setopt($chObj, CURLOPT_RETURNTRANSFER, true);