406-Fehler beim Senden/Empfangen von JSON-DatenPhp

PHP-Programmierer chatten hier
Guest
 406-Fehler beim Senden/Empfangen von JSON-Daten

Post by Guest »

Ich habe mehrere hier auf StackOverflow gefundene Optionen ausprobiert, konnte meinen Code jedoch nicht zum Laufen bringen. Da ich dachte, es sei einfach etwas Seltsames in meinem Code, habe ich ein paar einfache Skripte erstellt, um es auszuprobieren. Ich erhalte einen 406-Fehler und weiß nicht warum...
send.php

Code: Select all

$url = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://".$_SERVER['HTTP_HOST'].str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', realpath(__DIR__))."/receive.php";
$data = array(
'first_name' => "Bart",
'last_name' => "Simpson",
);

$return_data=json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Required for HTTP error codes to be reported via our call to curl_error($ch)
curl_setopt($ch, CURLOPT_POSTFIELDS, $return_data);
$resp = curl_exec($ch);
if (curl_errno($ch)) {
$ch_error_msg = curl_error($ch);
echo "ERROR:".$ch_error_msg;
exit;
}
curl_close($ch);

$result = json_decode($resp);
echo "
".var_export($result,true)."";
receive.php

Code: Select all

$response = file_get_contents('php://input');
$data = json_decode($response, true);
$data_display = var_export($data, true); // Nice readable view of array

if (isset($data['first_name']) && $data['first_name']!="")
{
http_response_code(200);
header("Content-Type:application/json");
$return_data["http_status_code"]="200";
$return_data["http_status_desc"]="OK";
echo json_encode($return_data);
exit;
}
else
{
http_response_code(400);
header("Content-Type:application/json");
$return_data["http_status_code"]="400";
$return_data["http_status_desc"]="Required data not supplied";
echo json_encode($return_data);
exit;
}
Warum erhalte ich einen 406-Fehler?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post