Example Script EPP PHP

From Openprovider API documentation

(Difference between revisions)
Jump to: navigation, search
WikiSysop (Talk | contribs)
(Created page with ' <?php $url = 'https://epp.cte.openprovider.eu'; $username = ""; $password = ""; $result = send( [ 'url' => $url, 'content' => '<?xml version="1.0"…')
Newer edit →

Revision as of 06:23, 19 April 2014

<?php

$url = 'https://epp.cte.openprovider.eu';
$username = "";
$password = "";

$result = send(
    [
        'url' => $url,
        'content' => '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
            <command>
                <login>
                    <clID>'.$username.'</clID>
                    <pw>'.$password.'</pw>
                    <options>
                        <version>1.0</version>
                        <lang>en</lang>
                    </options>
                    <svcs>
                        <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
                        <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
                        <svcExtension>
                            <extURI>http://www.openprovider.nl/epp/xml/opprov-1.0</extURI>
                        </svcExtension>
                    </svcs>
                </login>
                <clTRID>83EE2B34-2385-11DE-9267-8000000019CB</clTRID>
            </command>
        </epp>',
    ],
    $out
);
$doc = new DomDocument();
$doc->loadXML($result);
$xpath = new DomXpath($doc);
$node = $xpath->query("//*[local-name()='response']/*[local-name()='result']/@code");
$code = $node->item(0)->nodeValue;
if (1000 != $code) {
    die("Bad login/password");
}
$cookies = $out['cookies'];
$result = send(
    [
        'url' => $url,
        'content' => '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
                <command>
                    <check>
                        <domain:check xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
                            <domain:name>example.net</domain:name>
                            <domain:name>this-example-does-not-exist-1691.org</domain:name>
                        </domain:check>
                    </check>
                    <clTRID>83EE2B34-2385-11DE-9267-8000000019CC</clTRID>
                </command>
            </epp>',
        'cookies' => $cookies,
    ]
);

var_dump($result);

$result = send(
    [
        'url' => $url,
        'content' => '<?xml version="1.0" encoding="UTF-8"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
                <command>
                    <info>
                        <domain:info xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
                            <domain:name>iwantthisdomainname.net</domain:name>
                        </domain:info>
                    </info>
                    <clTRID>83EE2B34-2385-11DE-9267-8000000019CD</clTRID>
                </command>
            </epp>',
        'cookies' => $cookies,
    ]
);

var_dump($result);

$result = send(
    [
        'url' => $url,
        'content' => '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
                <command>
                    <logout/>
                    <clTRID>83EE2B34-2385-11DE-9267-8000000019CE</clTRID>
                </command>
            </epp>',
        'cookies' => $cookies,
    ]
);

var_dump($result);

function send ($args, &$out = null) {
    $url = $args["url"];
    $content = ($args["content"]) ?: null;
    $cookies = ($args["cookies"]) ?: null;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    if ($cookies) {
        curl_setopt($ch, CURLOPT_COOKIE, buildCookie($cookies));
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        die(
            "ERROR: code: ".curl_errno($ch)
            ."; error: `".curl_error($ch)."`."
        );
    }
    $header = explode("\n", substr(
        $result, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE)
    ));
    $out['cookies'] = parseCookie($header);
    $out['body'] = substr($result, curl_getinfo($ch, CURLINFO_HEADER_SIZE));

    curl_close($ch);

    return $out['body'];
}
function parseCookie ($header) {
    $cookies = array();
    foreach ($header as $line) {
        if (preg_match('/^Set-Cookie: /i', $line)) {
            $line = preg_replace('/^Set-Cookie: /i', "", trim($line));
            $csplit = explode(';', $line);
            $cdata = array();
            foreach ($csplit as $data) {
                $cinfo = explode('=', $data);
                $cinfo[0] = trim($cinfo[0]);
                $loweredCinfo = strtolower($cinfo[0]);
                if($loweredCinfo == 'expires') $cinfo[1] = strtotime($cinfo[1]);
                if($loweredCinfo == 'secure') $cinfo[1] = "true";
                if($loweredCinfo == 'httponly') $cinfo[1] = "true";
                if(in_array($loweredCinfo, array('domain', 'expires', 'path', 'secure', 'comment', 'httponly'))) {
                    $cdata[trim($cinfo[0])] = $cinfo[1];
                } else {
                    $cdata['value']['key'] = $cinfo[0];
                    $cdata['value']['value'] = $cinfo[1];
                }
            }
            $cookies[] = $cdata;
        }
    }
    return $cookies;
}
function buildCookie ($data) {
    if(is_array($data)) {
        $cookie = ;
        foreach($data as $d) {
            $cookie[] = $d['value']['key'].'='.$d['value']['value'];
        }
        if(count($cookie) > 0) {
            return trim(implode('; ', $cookie));
        }
    }
    return false;
}

?>
Views
Personal tools