带有 PHP 和 Curl 的 graphql

graph ql with PHP and Curl

我有身份验证工作,我什至已经在邮递员中测试了我的查询,但由于我似乎缺少我的查询的原因 returns

function generate_edge_curl(){
    $endpoint = $this->get_endpoint();
    $auth_token = $this->token->get_token();
    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'x-amz-user-agent: aws-amplify/2.0.1',
      'content-type: application/json',
      'Authorization: '.$auth_token['access_token'],
    ));
    return $ch;
  }

  function get_bidders(){
    $ch = $this->generate_edge_curl();
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{"query":"{
                  auctionInfo(id: \'alldal\') {
                        bidders {
                            list {
                                companyAmsId
                                companyName
                                firstName
                                lastName
                                badgeNum
                                bidderType
                            }
                        }
                    }
                  }"}');
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump($output);
  }

returns

    string(133) "{
      "errors" : [ {
        "message" : "Invalid JSON payload in POST request.",
        "errorType" : "MalformedHttpRequestException"
      } ]
}"

我是 graph ql 的新手,我在这里缺少什么?

所以经过一些挖掘和多次失败的尝试,这就是我想出的方法。

  function generate_edge_curl(){
    $endpoint = $this->get_endpoint();
    $auth_token = $this->token->get_token();
    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'x-amz-user-agent: aws-amplify/2.0.1',
      'content-type: application/json',
      'Authorization: '.$auth_token['access_token'],
    ));
    return $ch;
  }

  function get_bidders(){
    $query = <<<'GRAPHQL'
      query {
        auctionInfo(id: "alldal") {
          bidders {
            list {
              companyAmsId
              companyName
              firstName
              lastName
              badgeNum
              bidderType
            }
          }
        }
      }
    GRAPHQL;
    $ch = $this->generate_edge_curl();
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $query]));
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump($output);
  }

主要更改是以这种方式存储查询:

$query = <<<'GRAPHQL'
      query {
        auctionInfo(id: "alldal") {
          bidders {
            list {
              companyAmsId
              companyName
              firstName
              lastName
              badgeNum
              bidderType
            }
          }
        }
      }
    GRAPHQL;

然后 json_encoding 该字符串。 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $query]))

我从这里得到灵感https://gist.github.com/dunglas/05d901cb7560d2667d999875322e690a