2012-07-03 14 views
14

Ich versuche, JSON-Inhalt an einen Remote-REST-Endpunkt zu senden, der Inhalt scheint jedoch bei Lieferung leer zu sein. Alle anderen Header usw. werden korrekt empfangen und der Webdienst wird erfolgreich mit einem browserbasierten Testclient getestet.PHP - JSON über file_get_contents posten

Gibt es ein Problem mit meiner Syntax unten, wo ich das Feld 'Inhalt' angeben?

$data = array("username" => "duser", "firstname" => "Demo", "surname" => "User", "email" => "[email protected]"); 
$data_string = json_encode($data); 

$result = file_get_contents('http://test.com/api/user/create', null, stream_context_create(array(
'http' => array(
'method' => 'POST', 
'header' => array('Content-Type: application/json'."\r\n" 
. 'Authorization: username:key'."\r\n" 
. 'Content-Length: ' . strlen($data_string) . "\r\n"), 
'content' => $data_string) 
) 
)); 

echo $result; 

Antwort

16

Dies ist der Code, den ich immer ziemlich ähnlich verwenden und es sieht (obwohl dies natürlich für x-www-form-urlencoded). Vielleicht muss Ihre username:keybase64_encode 'd sein.

function file_post_contents($url, $data, $username = null, $password = null) 
{ 
    $postdata = http_build_query($data); 

    $opts = array('http' => 
     array(
      'method' => 'POST', 
      'header' => 'Content-type: application/x-www-form-urlencoded', 
      'content' => $postdata 
     ) 
    ); 

    if($username && $password) 
    { 
     $opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password")); 
    } 

    $context = stream_context_create($opts); 
    return file_get_contents($url, false, $context); 
} 
+1

Dank Das hat super funktioniert! – Ben

+4

Falls jemand auf das Problem stößt, dass die Post-Daten nicht korrekt codiert sind (jeder Schlüssel des Wörterbuchs hat am Anfang einen "amp;"): Ändern Sie die dritte Zeile in $ postdata = http_build_query ($ data, ' ',' &'); – Philipp

3

Die frühere Antwort von

function file_post_contents($url, $data, $username = null, $password = null) { 
$postdata = http_build_query($data); 

$opts = array('http' => 
    array(
     'method' => 'POST', 
     'header' => 'Content-type: application/x-www-form-urlencoded', 
     'content' => $postdata 
    ) 
); 

if($username && $password) 
{ 
    $opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password")); 
} 

$context = stream_context_create($opts); 
return file_get_contents($url, false, $context);} 

ist falsch. Diese Funktion funktioniert manchmal, aber sie ist ungenau und wird fehlschlagen, wenn Sie nicht den Inhaltstyp der Anwendung/x-www-form-urlencoded verwenden und einen Benutzernamen und ein Passwort eingeben.

Es funktioniert für den Verfasser, weil Anwendung/x-www-form-urlencoded der Standardinhaltstyp ist, aber seine Behandlung des Benutzernamens und des Kennworts überschreibt die frühere Deklaration des Inhaltstyps. Hier

ist die korrigierte Funktion:

function file_post_contents($url, $data, $username = null, $password = null){ 
$postdata = http_build_query($data); 

$opts = array('http' => 
    array(
     'method' => 'POST', 
     'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
     'content' => $postdata 
    ) 
); 

if($username && $password) 
{ 
    $opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password")); // .= to append to the header array element 
} 

$context = stream_context_create($opts); 
return file_get_contents($url, false, $context);} 

Beachten Sie die Zeile:. $ opts [ 'http'] [ 'header' = (. Punkt entspricht dem Array-Element anhängen)

Verwandte Themen