【发布时间】:2019-03-29 23:48:35
【问题描述】:
我需要使用 JSON 对象作为主体发出 POST 请求。这两种方法都给我 HTTP 500 服务器错误。我的代码有什么明显的错误吗?要温柔... 我尝试了几种方法,包括
$checkfor = ("'serverId':'Server','featureId':'Feature','propertyId':'Property'");
$checkforJson = json_encode($checkfor);
$uri = "http://localhost:8080/v1/properties";
$response = \Httpful\Request::post($uri)
->method(Request::post)
->withoutStrictSsl()
->expectsJson()
->body($checkforJson)
->send();
pre($response);
使用 HTTPful 资源。我尝试过使用 cURL
$service_url = "http://localhost:8080/v1/properties";
// Initialize the cURL
$ch = curl_init($service_url);
// Set service authentication
// Composing the HTTP headers
$body = array();
$body[] = '"serverId" : "Server"';
$body[] = '"featureId" : "Feature"';
$body[] = '"propertyId" : "Property"';
$body = json_encode($body);
$headers = array();
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml; charset=UTF-8';
// Set the cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
// Execute the cURL
$data = curl_exec($ch);
// Print the result
pre($data);
【问题讨论】:
-
太棒了。 500 个错误通常会告诉你代码哪里出错了。你的 error_log 说什么?
-
PHP 警告,json_encode() 期望参数 2 是 Apache 日志中的整数。并且服务器本身因 status.code unknown 出错(我知道有帮助)