【问题标题】:graph ql with PHP and Curl带有 PHP 和 Curl 的 graphql
【发布时间】:2020-12-14 16:00:12
【问题描述】:

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

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 的新手,我在这里缺少什么?

【问题讨论】:

    标签: curl graphql-php


    【解决方案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(){
        $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' =&gt; $query]))

    我从这里https://gist.github.com/dunglas/05d901cb7560d2667d999875322e690a得到了这个想法

    【讨论】:

    • 不错的收获!也为同样的问题而苦苦挣扎。谢谢!
    猜你喜欢
    • 2013-03-21
    • 2021-02-28
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多