Non-responsive PHP/XML script for EPP access

I’m trying to get started with using my hosting provider’s API access to their EPP. They provide certain information, which I’ve attempted to follow - without success!

For now I’m simply trying to send the greeting to the service, if I can get that going then I’ll take the next steps, but something isn’t working - and it here that I’m stuck.

[php]<?php

require_once(‘api.inc’);
$hi_api = new HeartInternet_API();
$hi_api->connect(false); // true = connect to the test API, false = connect to the live API.
$username = “myUsername”;
$password = “myPassword”;
$hi_api->logIn($username, $password, $objects, $extensions);

$some_xml = <<<XML

XML;

$returned_xml = $hi_api->sendMessage($some_xml, true);

echo "XML Sent:
" . htmlentities($some_xml) . “
”;
echo "XML Received:
" . htmlentities($returned_xml) . “
”;

?>[/php]

The api.inc file is as follows -

[php]<?php
/**

  • This provides easy access to the Heart Internet API. Please see

  • {@link http://api.heartinternet.uk/api2.html} for the API documentation

  • itself.

  • External methods:

  • $hi_api->connect(bool); // true = connect to the test API, false = connect to the live API. Default is false.

  • $hi_api->logIn($username, $password, $objects, $extensions);

  • $hi_api->sendMessage($xml_to_send, bool); // true = return array, false = return XML. Default is true.

  • Usage Example:

  • <?php
  • require_once(’…/HeartInternet_API.inc’);

  • $hi_api = new HeartInternet_API();

  • $hi_api->connect(true); // true = connect to the test API, false = connect to the live API.

  • $username = “your-username”;

  • $password = “your-password”;

  • $hi_api->logIn($username, $password, $objects, $extensions);

  • $some_xml = <<<XML

  • <?xml version="1.0"?>
  • (your command)

  • XML;

  • $returned_xml = $hi_api->sendMessage($some_xml, true);

  • echo "XML Sent:
    " . htmlentities($some_xml) . “
    ”;

  • echo "XML Received:
    " . htmlentities($returned_xml) . “
    ”;

  • ?>

  • Recent changes:

  • 0.2: Improved fread() usage for cases where there are over 8192/16384 bytes

  •  to read.
    
  • 0.3: Documentation updates - example usage, methods.

  • 0.4: Added local validation to logIn() to cover common error scenarios.

  • @copyright Copyright 2011, Heart Internet Ltd

  • @version 0.5
    /
    class HeartInternet_API {
    /
    *

    • @var string The main API namespace
      /
      public $namespace = “urn:ietf:params:xml:ns:epp-1.0”;
      private $hostname = “api.heartinternet.uk”;
      /
      *
    • Connects to the API server and returns the greeting (as literal XML).
    • @param boolean $test_mode Set to true if you want to connect to the test service instead.
      /
      public function connect($test_mode=false) {
      $this->res = fsockopen(“tls://” . $this->hostname, $test_mode ? 1701 : 700);
      return $this->getResponse();
      }
      function getResponse() {
      $size_packed = fread($this->res, 4);
      if(strlen($size_packed) == 0) return;
      $size = unpack(“N”, $size_packed);
      // This works around PHP’s fread() capping at 8192 bytes
      $out = “”;
      $last = “”;
      for($s = $size[1]-4; $s>0; $s-=strlen($last)) {
      $last = fread($this->res, $s);
      $out.=$last;
      }
      return $out;
      }
      /
      *
    • This sends an XML message to the API, and returns the result, as an
    • array by default. This will throw an exception in the case of an internal failure.
    • @param string $output The XML message to send
    • @param boolean $no_parsing Set to true if you want the raw XML response returned.
      */
      public function sendMessage($output, $no_parsing=false) {
      fwrite($this->res, pack(“N”, strlen($output)+4) . $output);
      $content = $this->getResponse();

    if($content) {
    if($no_parsing) {
    return $content;
    } else {
    $result=array();
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $content, $result);
    // Do something with $result
    return $result;
    }
    } else {
    // Error handling
    throw new Exception(“Communication failure”);
    return;
    }
    }
    /**

    • Logs you in (once connected). Will raise an exception on failure.
    • @param string $userid Your API user ID, see the API documentation.
    • @param string $password Your API password.
    • @param array $objects The object namespaces to load. Required.
    • @param array $extensions the extension namespaces to load.
      */
      public function logIn($userid, $password, $objects, $extensions) {
      if(!preg_match(’/^[a-f0-9]+$/’, $userid)) {
      throw new Exception(
      “Invalid username, should look like ‘9cf2cdbcce5e00c0’”
      );
      }

    if( (!$objects) || empty($objects) ) {
    throw new Exception(
    “You must provide some object namespaces, please see the login examples in the documentation”
    );
    }

    $doc = new DOMDocument();
    $content = $doc->createElement(“login”);
    $clID_element = $doc->createElement(‘clID’);
    $clID_element->appendChild(
    $doc->createTextNode($userid)
    );
    $content->appendChild($clID_element);

    $pw_element = $doc->createElement(‘pw’);
    $pw_element->appendChild(
    $doc->createTextNode($password)
    );
    $content->appendChild($pw_element);

    $options_element = $doc->createElement(‘options’);

    $version_element = $doc->createElement(‘version’);
    $version_element->appendChild( $doc->createTextNode(“1.0”));
    $options_element->appendChild($version_element);

    $lang_element = $doc->createElement(‘lang’);
    $lang_element->appendChild( $doc->createTextNode(“en”));
    $options_element->appendChild($lang_element);

    $content->appendChild($options_element);

    $svcs_element = $doc->createElement(‘svcs’);

    foreach($objects as $object) {
    $element = $doc->createElement(‘objURI’);
    $element->appendChild($doc->createTextNode("$object"));
    $svcs_element->appendChild($element);
    }

    $svcs_extensions = $doc->createElement(‘svcExtension’);
    foreach($extensions as $extension) {
    $element = $doc->createElement(‘extURI’);
    $element->appendChild($doc->createTextNode("$extension"));
    $svcs_extensions->appendChild($element);
    }
    $svcs_element->appendChild($svcs_extensions);

    $content->appendChild($svcs_element);

    $xml = $this->buildXML($content);
    $result = $this->sendMessage($xml);
    // error_log($xml);
    foreach($result as $tag) {
    if(
    $tag[‘tag’]==“result” &&
    $tag[‘type’]!=“close” &&
    $tag[‘attributes’][‘code’]!=1000
    ) throw new Exception("Failed to log in!: " . $tag[‘attributes’][‘code’]);

    if($tag[‘tag’]==“session-id”) return $tag[‘value’];
    }
    return $result;
    }
    /**

    • This transforms a DOMDocument for the inner part of the request (inside
    • ) into an XML string.
    • @param DOMDocument $content
      */
      public function buildXML($content) {
      $doc = $content->ownerDocument;
      $epp = $doc->createElement(“epp”);
      $epp->setAttribute(“xmlns”, $this->namespace);
      $doc->appendChild($epp);

    $c = $doc->createElement(‘command’);
    $epp->appendChild($c);
    $c->appendChild($content);

    $output = $doc->saveXML();
    return $output;
    }

/**
 * Disconnects from the API server.
 */
public function disconnect() {
  fclose($this->res);
}

}
?>[/php]

Can anyone work out why this doesn’t work? Apologies in advance if this is really simple, but thanks!

What does it currently do?

Do you have a documentation link for the class?

Currently it does nothing :frowning:

Attempting in Chrome gets me this -

The thisismatt.co.uk page isn’t working

thisismatt.co.uk is currently unable to handle this request.
HTTP ERROR 500

Sorry, showing my newbie status here, but what do you mean about class? :-/

The only docs I have are what’s in the .INC file and then on the API pages of Heart Internet.

First, change the extension from inc to php.

Then try again.

No joy, same result.

Your XML is poorly formed.

Try their example first; and what is the command you want to send?

[php]$some_xml = <<<XML
<?xml version="1.0"?>


Hello


XML;
[/php]

Thanks. Admittedly I have just copy/pasted the XML.

As far as I can understand, their example is complete, sending just the should suffice, according to their documentation (http://api.heartinternet.co.uk/api2.html)

The page (http://thisismatt.co.uk/epp/phptest.php) does now provide errors (now that I’ve found out how to do that!)…

That is because of all of the extra variables you are passing in,

[php]$hi_api->logIn($username, $password, $objects, $extensions);[/php]

You aren’t defining, or doing anything with these variables, $objects, $extensions.

Sponsor our Newsletter | Privacy Policy | Terms of Service