Sunshine reggae - Репутация: 1830
- Webmoney BL:
? | kvins, выдрал из одного скрипта для работы с потоковым видео, но суть та же
Код: // build xml
$xml = '
<?xml version="1.0" encoding="UTF-8"?>
<file>
<auth>Ваш ключ API</auth>
<countPrice>
<senderCity>Полтава</senderCity>
<recipientCity>Киев</recipientCity>
<mass>20</mass>
<height>10</height>
<width>5</width>
<depth>10</depth>
<publicPrice>100</publicPrice>
<deliveryType_id>1</deliveryType_id>
<floor_count>4</floor_count>
<date>19.01.2012</date>
</countPrice>
</file>';
// отправляем запрос - $responce сохраняет ответ
$response = http_post_curl('text/xml', $xml);
if($response === FALSE) {
return FALSE;
}
function http_post_curl($content_type, $post_data)
{
$url = 'http://orders.novaposhta.ua/xml.php';
// init curl
$curl = curl_init($url);
if(!$curl) {
$this->_add_error('Failed to init cURL');
return FALSE;
}
// curl options
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
$http_headers = array(
'Content-type: ' . $content_type
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
if($response === FALSE) {
return FALSE;
}
return $response;
} В том же файле разбирается ответ функцией simplexml_load_string Вот код функции разбора, не охота ковырять под вашу задачу, сами как нибудь :)
Код: function _parse_response($response)
{
$status = '';
$error_code = 0;
$error = '';
$content_uid = '';
if(function_exists('domxml_open_mem')) {
// load XML
$doc = domxml_open_mem($response);
if( ! $doc) {
$this->_add_error('Failed to parse XML: ' . $response);
return FALSE;
}
// find <status>
$list = $doc->get_elements_by_tagname('status');
if(count($list) == 0) {
$this->_add_error('Unable to find <status> in response: ' . $response);
return FALSE;
}
$elem_status = $list[0];
// get <status> value
if($elem_status->has_child_nodes()) {
$text_node = $elem_status->first_child();
if($text_node->type == XML_TEXT_NODE) {
$status = $text_node->content;
}
}
// check 'errorCode' and 'error' attributes
if($elem_status->has_attribute("error")) {
$error = $elem_status->get_attribute("error");
}
if($elem_status->has_attribute("errorCode")) {
$error_code = $elem_status->get_attribute("errorCode");
}
// find <id>
$list = $doc->get_elements_by_tagname('id');
if(count($list) != 0) {
$elem_id = $list[0];
// get <id> value
if($elem_id->has_child_nodes()) {
$text_node = $elem_id->first_child();
if($text_node->type == XML_TEXT_NODE) {
$content_uid = $text_node->content;
}
}
}
}
elseif(function_exists('simplexml_load_string')) {
// load XML
$doc = simplexml_load_string($response);
if( ! $doc) {
$this->_add_error('Failed to parse XML: ' . $response);
return FALSE;
}
// find <status>
if( ! isset($doc->status)) {
$this->_add_error('Unable to find <status> in response: ' . $response);
return FALSE;
}
$elem_status = $doc->status;
$attributes = $elem_status->attributes();
// get <status> value
$status = (string)$doc->status;
// check 'errorCode' and 'error' attributes
if(isset($attributes['error'])) {
$error = (string)$attributes['error'];
}
if(isset($attributes['errorCode'])) {
$error_code = (string)$attributes['errorCode'];
}
// find <id>
if(isset($doc->id)) {
// get <id> value
$content_uid = (string)$doc->id;
}
}
else {
$this->_add_error('Cannot parse response from server: XML library must be installed');
return FALSE;
}
// return depending on status
if($status == 'accepted') {
if(strlen($content_uid) == 0) {
$this->_add_error('Empty content id in response: ' . $response);
return FALSE;
}
return $content_uid;
}
elseif($status == 'failed') {
$this->_add_error('Response failed: errorCode=' . $error_code . ' error=' . $error);
return FALSE;
}
else {
$this->_add_error('Unknown status (' . $status . ') in response: ' . $response);
return FALSE;
}
} Последний раз редактировалось OKyJIucT; 10.09.2013 в 16:46. |
|