2016-01-11 22 views
6

Desidero eseguire un post con guzzle che invia un file xml. Non ho trovato un esempio.Modo corretto per inviare (POST) xml con guzzle 6

cosa 'ho fatto finora è:

$xml2=simplexml_load_string($xml) or die("Error: Cannot create object"); 
use GuzzleHttp\Client; 
use GuzzleHttp\Psr7\Request; 
$client = new Client(); 
// 
$request = new Request('POST', $uri, [ 'body'=>$xml]); 
$response = $client->send($request); 
// 
//$code = $response->getStatusCode(); // 200 
//$reason = $response->getReasonPhrase(); // OK 
// 
echo $response->getBody(); 

Non importa quello che provo torno errore -1 che significa che XML non è valido. XML che invio passa la convalida online ed è valido% 100

Please help.

risposta

12

Dopo alcuni esperimenti, ho capito. Ecco la mia soluzione nel caso qualcuno raggiunga un vicolo cieco.

$request = new Request(
    'POST', 
    $uri, 
    ['Content-Type' => 'text/xml; charset=UTF8'], 
    $xml 
); 
-2

prova a pubblicare i dati come:

$xml2=simplexml_load_string($xml) or die("Error: Cannot create object"); 
use GuzzleHttp\Client; 
use GuzzleHttp\Psr7\Request; 
$client = new Client(); 
// 
$request = new Request('POST', $uri, [ 
'form_params' => [ 
     'xml' => $xml, 
    ] 
]); 
$response = $client->send($request); 
//$code = $response->getStatusCode(); // 200 
//$reason = $response->getReasonPhrase(); // OK 
echo $response->getBody(); 
+0

Grazie per il suggerimento ma non funziona. Di nuovo la stessa risposta. Esiste qualche documentazione che descrive in dettaglio l'oggetto delle opzioni? – user3485417

1

Se si desidera inviare XML utilizzando il metodo post, ecco un esempio:

$guzzle->post($url, ['body' => $xmlContent]); 
1

Questo è ciò che ha funzionato per me su Guzzle 6:

$Options = [ 
    'headers' => [ 
     'ContentT-ype' => 'text/xml; charset=UTF8', 
    ], 
    'body' => $XML, 
]; 

$Response = $Client->request('POST',$Url,$Options); 
Problemi correlati