Wie die post-Daten mit curl und bekommen Antwort, basierend auf gebuchten Daten

Hier ist mein code, um POST Daten:

<?php
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$data_string = json_encode($data);

$url = 'http://mydomain.com/curl.php';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
?>

<p>Your confirmation number is: <strong><?php echo $json_result['ConfirmationCode']; ?></strong></p>

In der Erwägung, dass auf domain/server curl.php Datei-code unter:

<?php
//header 
header("content-type: application/json");

if($_POST):
    echo json_encode(array('ConfirmationCode' => 'somecode'));
else:
    echo json_encode(array('ConfirmationCode' => 'none'));
endif;
?>

Aber es immer wieder 'none'. Bin ich etwas fehlt?

InformationsquelleAutor atif | 2013-03-13

Schreibe einen Kommentar