Webonyx/Graphql-php 入门:如何在没有 API 实现回声的情况下获取 cURL 中 API 的响应?

亚历克斯·阿布雷乌骡子

我是 GraphQL 的新手,想构建一个简单的 API 来开始使用。阅读文档并尝试示例后,API 终于可以正常工作了……但是!!!

API 的 php 实现以“echo”结尾(这对于 Graphiql 客户端来说很好用!),但是当我尝试在 cURL 中获取响应时,这个 echo 出现在我的页面源中......

请伙计们,如何避免这种回声并在cURL中获得结果?我求助于巨大的集体智慧来获得一些帮助。

以下是我使用的资源:

这是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":"user name"}}}来自 API 实现echo json_encode($output);

我尝试使用 get_file_content 而不是 cURL 但无法取得良好的效果,请提供任何帮助,我们将不胜感激!

阿米尔·MB

设置CURLOPT_RETURNTRANSFER为 true 将传输作为 curl_exec() 的返回值的字符串返回,而不是直接输出。

curl_setopt($chObj, CURLOPT_RETURNTRANSFER, true);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在没有cURL的情况下如何在PHP中与API服务之间来回发送和接收XML数据

在没有cURL的情况下在PHP中调用API

MailChimp API在V3.0 PHP Curl中响应404

如何从 CURL 响应 PHP API 的 JSON 字符串中删除引号、不需要的数据

无法在 PHP 中使用 curl 从 API 获取 json 响应

PHP-如何在没有API访问令牌的情况下从Instagram获取图像

PHP在没有API的情况下获取Youtube注释

如何使用 KlaviyoClient 在没有 Http Get 的情况下从响应 API 获取状态代码?

使用cURL在PHP中获取api数据

PHP 中带有 CURL 的 API

如何在React中获取API响应?

浏览PHP中的api响应

如何使用 cURL 修复表单并从 api 获取响应

Web API如何在不托管的情况下响应?

如何在 php 中通过 Curl 正确使用 Mailgun Api

如何在 PHP 中运行(api-request)静态 URL 并获取响应?

如何从API响应中获取访问对象?

如何从api的响应中获取正确的输出?

如何从以下 api 响应中获取特定对

在不使用私有API的情况下获取当前的第一响应者

如何从php做出API响应

如何在没有外部库的情况下在React中实现Google Maps JS API?

如何在没有 Spark Rest API 的情况下监听 Spark 中的作业进度

PHP从API响应中获取json汇率值

在 chrome/firefox 扩展 API 中,如何在不发送新消息的情况下从端口获得链接响应?

cURL获取API数据以显示在php表中

Google API curl在php中获取刷新令牌

如何从 PHP 中的 API 响应动态访问数组的值?

在没有API的情况下通过PHP脚本获取Minecraft uuid