PHP和Curl的图形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,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的人,我在这里想念什么?

解决方法

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

  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,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,json_encode(['query' => $query]))

我从这里https://gist.github.com/dunglas/05d901cb7560d2667d999875322e690a