【发布时间】:2020-09-20 11:04:44
【问题描述】:
我有这个突变,允许我在 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);
【问题讨论】:
-
500 错误是一般的服务器错误。您应该检查您的 Web 服务器错误日志以查看实际错误消息的内容。如果您在本地工作,您可以让errors and warnings 直接显示在屏幕上。
-
谢谢@MagnusEriksson,您的建议帮助我找出了错误。只是一个非常愚蠢的错误......我在
string $token =null之前有一个问号 -
“我有一个问号”:这个问号是 PHP 7.1 的 nullable type 的指示符。