PHP nasıl bir POST değişken bir XML dosyası ayrıştırmak?

1 Cevap php

Benim sunucuya bir XML isteği gönderdiğinde bir senaryo yazdım. Aşağıya bakın:

$xml_request='<?xml version="1.0"?><request><data></data></request>';
$url='http://www.myserver.com/xml_request.php';

//Initialize handle and set options

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

$result = curl_exec($ch); 

curl_close($ch);

Şimdi sunucu tarafında bu isteği ayrıştırmak için nasıl anlamaya çalışıyorum. Ben basit yaparsanız

print_r($_POST);

döndürür:

Array
(
    [<?xml_version] => \"1.0\"?><request><data></data></request>
)

doğru görünmüyor hangi. Büyük olasılıkla () simplexml_load_string, bu yüzden temiz XML dosyasına erişimi gerekir - Ben XML ayrıştırıcıların birine yazı geçmek mümkün olmak istiyorum. Temiz bir dosya olsun nasıl POST isteğini girebilirim?

1 Cevap

The problem is with the script that posts the request. You need to encode the XML string before you send it to the server in order to prevent the server from parsing it. Try running your XML through rawurlencode before you post it to the server.


Bu daha içine bakıyor, ben başka bir sorun gördüm. CURLOPT_POSTFIELDS seçenek dize kendinizi oluşturmak için bekliyor gibi görünüyor. Quoth the manual page (kalın benim):

Tam veri HTTP "POST" operasyonu sonrası için. Bir dosyayı göndermek için, @ ile bir dosya adı Önlerine ve tam yolunu kullanın. Değer bir dizi This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value., Content-Type üstbilgisi / form-data multipart ayarlanır.

Yani senin durumunda, bunun gibi bir şey olurdu

curl_setopt($ch, CURLOPT_POSTFIELDS, 'xmlstring='.rawurlencode($xml_request))

Ve sonra alıcı komut, size $ _POST ['xmlString'] olarak erişebilir olmasını beklersiniz.