PHP GraphQL 语法错误使用 file_get_contents 发出查询

问题描述 投票:0回答:0

我正在尝试开发一个简单的 php 客户端,但遇到了一些麻烦。 我的代码是这样的:

    $Tok = 'my token';
    $Url = 'the api endpoint url';
    $ItemId = '$book - 093121'; // This is the id I search for
    $headers = ['Content-Type: application/json', 'Authorization: Bearer ' . $Tok];
    $query = 'query product("id":"$ItemId")';
    $data = file_get_contents($Url, false, stream_context_create([
     'http' => [
      'method' => 'POST',
      'header' => $headers,
      'content' => json_encode(['query' => $query]),
     ]
    ]));
    $responseContent = json_decode($data, true);
    echo json_encode($responseContent);

运行脚本我收到以下信息:

{"errors":[{"message":"Line 1 col 18: Syntax error"}]}

当然,我尝试了很多组合('id' 和/或 $ItemID 上没有引号,结果总是一样的,也许有不同的“col”

API开发者发布的文档说:

query product($id: IdInput!) {
  product(id: $id) {
    id
    ts
    visible
    title
    slug
    description
    stock
    allocated
    price
    productClass
    codiceIva
    discount
    weight
    details {
      ...ProductDetailFragment
    }
  }
}

Variables
{"id": IdInput}

有人可以帮我吗? 提前谢谢你

php graphql file-get-contents
© www.soinside.com 2019 - 2024. All rights reserved.