Shopify GraphQL 错误 "Parse error on \":\" (COLON) at [2, 35]" With PHP

Shopify GraphQL Error "Parse error on \":\" (COLON) at [2, 35]" With PHP

您好我的 GraphQL 有问题我不知道如何将数据传递到我的 GraphQL 而不获取

错误消息:“[2, 35] 处“:”(COLON) 的解析错误”

这是我尝试传递产品变体 ID 数据并获得一些响应的示例,这是我尝试做的示例以及我的 graphql 函数

$variantId = (isset($data->variantId) && !empty($data->variantId)) ? strip_tags($data->variantId) : "";

        if(empty($variantId)){
            $result['error'] = "Product id not specified!";
        }

        $query = array("query" => '{
            productVariant(id: '. ($variantId) .') {
                availableForSale
            }
        }');

        $variants = shopify_gql_call($_SESSION['access_token'], $_SESSION['shop_name'], $query);
        if( isset($variants['response']) && !empty($variants['response']) ){
            $result[]  = $variants['response'];
        }else{
            $result['error'] = "Variants not found!";
        }

function shopify_gql_call($token, $shop, $query = array()) {
    // Build URL
    $url = "https://" . $shop . ".myshopify.com" . "/admin/api/".getenv('API_DATE')."/graphql.json";

    // Configure cURL
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
    // curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);

    // Setup headers
    $request_headers[] = "";
    $request_headers[] = "Content-Type: application/json";
    if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($query));
    curl_setopt($curl, CURLOPT_POST, true);

    // Send request to Shopify and capture any errors
    $response = curl_exec($curl);
    $error_number = curl_errno($curl);
    $error_message = curl_error($curl);

    // Close cURL to be nice
    curl_close($curl);

    // Return an error is cURL has a problem
    if ($error_number) {
        return $error_message;
    } else {

        // No error, return Shopify's response by parsing out the body and the headers
        $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);

        // Convert headers into an array
        $headers = array();
        $header_data = explode("\n",$response[0]);
        $headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
        array_shift($header_data); // Remove status, we've already set it above
        foreach($header_data as $part) {
            $h = explode(":", $part, 2);
            $headers[trim($h[0])] = trim($h[1]);
        }

        // Return headers and Shopify's response
        return array('headers' => $headers, 'response' => $response[1]);

    }

  }

我强烈建议使用 https://packagist.org/packages/shopify/shopify-api 而不是实施您自己的 function/http 请求。

您的查询应该是这样的

query anynamehere($id: ID!){
  productVariant(id:$id){
    availableForSale
  }
}

然后您将 ID 作为数组另一个条目的一部分提交,请查看以下示例:

  $query = [
    "query" => 
        'query anynamehere($id: ID!){
          productVariant(id:$id){
            availableForSale
          }
        }',
    "variables"  => [
        'id' => $variantId
    ]
  ];

永远不要将值连接为查询字符串的一部分(除非您想处理很多注入问题)。在此处查看有关变量的更多信息 https://graphql.org/learn/queries/