php - 为什么我的带有 graphQL 的 php 代码会出现 500 错误?
问题描述
我有这个突变,允许我在 MongoDB 的集合中插入一个客户端。当我创建函数时,php会抛出错误500。这对我来说很奇怪,因为它发生在我编写函数时,而不是当我调用它时
功能是:
function graphQLPost(string $endpoint, string $query, array $variables = [], ?string $token = null)
{
$payload = json_encode(['query' => $query, 'variables' => $variables]);
$headers = array();
if ($token != null) {
$headers[] = "Content-Type: application/json\r\n" .
'Authorization: Bearer ' . $token;
} else
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Submit the POST request
$resultJson = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL session handle
curl_close($ch);
return array($resultJson, $httpcode);
}
// definiamo la mutation
$mutation = '
mutation createClients($project_id: ID!, $input: ClientInput!) {
createClients(project_id: $project_id, input: $input) {
id
firstName
lastName
tel
email
trattamento
profilazione
marketing
}
} ';
// parametri da passare come argomenti
$param = [
'project_id' => $PROJECT_ID,
'input' => array(
'firstName' => $nome,
'lastName' => $cognome,
'email' => $email,
'tel' => $telefono,
'trattamento' => $trattamento_DB,
'profilazione' => $profilazione_DB,
'marketing' => $marketing_DB,
'status' => 'lead',
)
];
// esecuzione del comando
$response = graphQLPost($API_URI, $mutation, $param, $TOKEN);
解决方案
找出原因。我在这里犯了一个愚蠢的语法错误?string $token = null
问号不是必需的。
推荐阅读
- powershell - 为什么 Format-Table 省略属性
- java - 乐观锁定失败;嵌套异常是 org.hibernate.StaleObjectStateException
- c# - WPF HierarchicalDataTemplate 将 ItemSource 绑定到“嵌套”类属性
- html - 如何使用关键帧自动为我的 css 效果设置动画?
- cucumber - 如何在独立 jar 中定义或使用 karate-config.js?
- bash - 使用 xargs 限制作业提交
- firebase - Firebase 在动态链接部分显示不同的捆绑包 ID
- html - 如何使用 slideDown() Jquery 制作动画
- azure - 来自 REST API 调用的错误消息未显示在 B2C 自定义策略中
- linkedin - 使用 ugcPosts API 的linkedin 视频没有缩略图