graph ql with PHP and Curl

Aaron Blakeley

I have the authentication working and I have even tested my query in postman yet for reasons that I seem to be missing my query 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"
      } ]
}"

I am brand new to graph ql what am I missing here?

Aaron Blakeley

So after some digging and many failed attempts this is what I came up with that worked.

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

The major changes were storing the query this way:

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

and then json_encoding that string. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $query]))

I got the idea from here https://gist.github.com/dunglas/05d901cb7560d2667d999875322e690a

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事