【发布时间】:2022-01-22 15:53:50
【问题描述】:
努力弄清楚如何使用 PHP 进行 graphql api 调用。我正在尝试使用 tezos 域 API 将 .tez 域转换为地址。我的查询在他们的“操场”中运行良好,只是无法将其翻译成 php。
尝试 1:
$apiUrl = 'https://api.tezos.domains/graphql';
$query = '{
domains(where: { name: { in: "' . $wallet . '" } }) {
items {
address
}
}
}';
$data = @file_get_contents($apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'content' => json_encode(['query' => $query]),
]
]));
$results = json_decode($data, true);
echo $results;
$wallet = $results['data'];
尝试 2:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.tezos.domains/graphql" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"query":"{domains(where: {name: { in:"elli0t.tez"}}) {items {address}}}"}');
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Accept-Encoding: gzip, deflate, br',
'Content-Type: application/json',
'Accept: application/json',
'Connection: keep-alive',
'DNT: 1',
'Origin: https://api.tezos.domains' )
);
$result = curl_exec($ch);
print_r($result);
$info = curl_getinfo($ch);
print_r($info);
尝试 2 至少给我返回了一些东西。它说我错过了一个查询。有什么想法吗?
这是从 api 的 Playground 生成的 curl 命令,似乎可以正常工作。我只是无法将其转换为 PHP:
curl 'https://api.tezos.domains/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://api.tezos.domains' --data-binary '{"query":"{\n domains(where: { name: { in: \"elli0t.tez\" } }) {\n items {\n address\n }\n }\n}"}' --compressed
【问题讨论】: