PHP POST 使用 CURL 和 JSON 内容。错误 500

PHP POST using CURL and JSON content. Error 500

我找到了一些关于post和使用curl获取方法的例子,但是我无法处理服务器内部错误500。google控制台报告获取类型的错误500。 我检查了我的 php 配置,似乎没问题。 上下文:基于 elementor 模板的 wordpress 安装 functions.php 中的函数。 Ionos 托管。我该如何调试这个问题?

      $url = 'https://myurl.com/';
    
      $fields = array('var1' => 'value1', 'var2' => 'value2');
      $headers = array('X-MY-TOKEN: tokenValue', 'Content-Type: application/json');
    
      $fields_json = json.encode($fields);
      
      //open connection
      $ch = curl_init();
    
      //setup
      curl_setopt($ch,CURLOPT_URL, $url);
      curl_setopt($ch,CURLOPT_POST, 1);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_json);
      curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
    
      //execute post
      $response = curl_exec($ch);

更新:php 日志错误显示:PHP Fatal error: Uncaught Error: Call to undefined function encode() 如何发送内容类型为 application/json 的 POST?

根据服务器抛出的错误,函数 encode() 不能find/doesn不存在,returns 500 错误代码。我假设你打错了 json_encode().

所以应该是:

$fields_json = json_encode($字段);

来自文档。 https://www.php.net/manual/en/function.json-encode.php