首页 > 解决方案 > 为什么我的带有 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);

标签: phpgraphqlmutation

解决方案


找出原因。我在这里犯了一个愚蠢的语法错误?string $token = null

问号不是必需的。


推荐阅读