1

我的身份验证工作正常,我什至在邮递员中测试了我的查询,原因是我似乎错过了我的查询返回

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

返回

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

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

4

1 回答 1

2

因此,经过一些挖掘和许多失败的尝试,这就是我想出的有效方法。

  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

于 2020-08-26T16:13:31.997 回答