GraphQL using curl




    The code uses curl to query GraphQL. The first three parameters are the URL to query, the GraphQL query, and the optional options. The fourth parameter is the $opts variable, an array of optional options. The fifth and sixth parameters are the $headers and $body variables, respectively. The curl_init() function initializes the curl object. The curl_setopt() function sets the various curl options. The most important option is CURLOPT_URL, which sets the URL to query. The next three options are used to set the HTTP headers. The CURLOPT_HTTPHEADER argument sets the HTTP header. The CURLOPT_POST argument sets the request to be a POST request. The CURLOPT_POSTFIELDS argument sets the body of the POST request. Finally, the CURLOPT_RETURNTRANSFER flag sets the return value of the curl request to be a stream. The curl_setopt() function sets all of the options, and then returns the curl object.

    Shortcut: net.graphqlUseCurl

    * query GraphQL using php curl
    function getGraphQLCurl(string $url, string $query, $opts=[])
      $headers = [
        "Content-Type: application/json",
        // "Accept: application/vnd.github.v4.idl",
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0",
      if (isset($opts['bearerToken'])) {
        $headers[] = "Authorization: bearer {$opts['bearerToken']}";
      $body = ["query" => $query];
      // create curl request
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
      curl_setopt($ch, CURLOPT_VERBOSE, false);
      return $ch;
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.