我想向“The Graph”API 发出 PHP POST 请求。我一直找不到这样的例子。
例如,我如何将 Polymarket 的示例查询转换为 PHP POST 请求? https://thegraph.com/explorer/subgraph/tokenunion/polymarket-matic?selected=playground
{
globals(first: 5) {
id
numConditions
numOpenConditions
numClosedConditions
}
accounts(first: 5) {
id
creationTimestamp
lastSeenTimestamp
collateralVolume
}
}
到目前为止我有
$sUrl="https://api.thegraph.com/subgraphs/name/tokenunion/polymarket-matic";
$query="query {
globals(first: 5) {
id
numConditions
numOpenConditions
numClosedConditions
}
accounts(first: 5) {
id
creationTimestamp
lastSeenTimestamp
collateralVolume
}
}";
$aData=array('query' => $query);
$options = array(
'https' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($aData)
)
);
$context = stream_context_create($options);
$result = file_get_contents($sUrl, false, $context);
var_dump($result);
这只是返回一个GraphiQL示例。
答案 0 :(得分:1)
我不确定我做错了什么。也许是语法问题?但是我找到了另一个例子并且能够修改它以工作。所以这是有效的(为用户返回一个多市场交易列表,从 2020 年开始,限制为 1000):
$sUrl="https://api.thegraph.com/subgraphs/name/tokenunion/polymarket-matic";
$sStartdate='2020/01/01';
$sEnddate='2020/12/31';
$iStarttime=strtotime($sStartdate);
$iEndtime=strtotime($sEnddate);
$sUser="0xf449b747376a2036dd76ada1484e0a2389c8d43b";
$sGraph='{"query":"{transactions(where: {user:\"'.$sUser.'\" timestamp_gte: '.$iStarttime.' timestamp_lte: '.$iEndtime.'} orderBy: timestamp orderDirection: asc first:1000) {id, type, timestamp, market {id}, tradeAmount, feeAmount, outcomeIndex, outcomeTokensAmount}}"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $sGraph);
curl_setopt($ch, CURLOPT_URL, $sUrl);
$data = curl_exec($ch);
curl_close($ch);
$aData=json_decode($data);
print_r ($aData);