【问题标题】:why does my php code with graphQL is trowing a 500 error?为什么我的带有 graphQL 的 php 代码会出现 500 错误?
【发布时间】: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 的指示符。

标签: php graphql mutation


【解决方案1】:

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

问号不是必须的。

【讨论】:

  • 使用 php 7.1+ 代码 [功能] 在具有较低 php 版本解释器的服务器上使用...不是语法错误 ;)
  • 它允许传递nullstring(从 php 7.1 开始)。它叫nullable types
猜你喜欢
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多