B0Bka / box

0 stars 0 forks source link

нова пошта #16

Open B0Bka opened 7 years ago

B0Bka commented 7 years ago
<?
namespace Webvision\Main;
class Np
{
    public $api_key;

    public function __construct($api_key)
    {
        $this->api_key = $api_key;
    }
    function get_city_list($filter = false)
    {
        $json = $this->get_json_sity();
        $result_json = $this->make_request_2($json);
        return json_decode($result_json);
    }
    function get_warehouses($filter)
    {
            //$xml = $this->make_xml('warehouses', $filter);
        //$result_xml = $this->make_request($xml);
        //return simplexml_load_string($result_xml);
        $json = $this->get_json_warehouses($filter);
        $result_json = $this->make_request_2($json);
        return $result_json;
    }
    function make_request_2($json)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://testapi.novaposhta.ua/v2.0/json/AddressGeneral/getWarehouses');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
    function get_json_sity($Page=1)
    {
        $arr = array(
            'apiKey' => $this->api_key,
            'modelName' => 'Address',
            'calledMethod' => 'getCities',
            'methodProperties' => array('Page'=>$Page)
        );
        return json_encode($arr);
    }
    function get_json_warehouses($Ref)
    {
    //  if(!strlen($Ref)) return false;
        $arr = array(
            'apiKey' => $this->api_key,
            'modelName' => 'Address',
            'calledMethod' => 'getWarehouses',
            'methodProperties' => array('CityRef'=>$Ref)
        );
        return json_encode($arr);
    }
    function make_request($xml)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://orders.novaposhta.ua/xml.php');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
    function object_to_array($obj)
    {
        $data = json_decode(json_encode(simplexml_load_string($obj)), true);
        return $obj;
    }
    function make_xml($select, $filter = false)
    {
        $dom = new DOMDocument("1.0", "utf-8");
        $xml_root = $dom->createElement("file");
        $xml_auth = $dom->createElement("auth", $this->api_key);
        $xml_root->appendChild($xml_auth);
        switch($select)
        {
            case 'city':
                $xml_city = $dom->createElement("citywarehouses");
                $xml_root->appendChild($xml_city);
                break;
            case 'warehouses':
                $xml_warehouses = $dom->createElement("warenhouse");
                $xml_root->appendChild($xml_warehouses);
                break;
        }
        if($filter)
        {
            $xml_filter = $dom->createElement("filter", $filter);
            $xml_root->appendChild($xml_filter);
        }
        $dom->appendChild($xml_root);
        return $dom->saveXML();
    }
}

$WVNovaPoshta = new Webvision\Main\Np('6ece38edc821aeddb937ec590fe86bd8');
$objWarehouseList = $WVNovaPoshta->get_warehouses();
?>