Now with this structure where we need to instantiate the connection?
I’m asking this because I’m getting an error…
From the most abstract to the less abstract, so I hope:
ConexaoEpp.Class.php
class ConexaoEpp
{
//TO TEST: IF I CHANGE TO PRIVATE WHAT WILL HAPPEN ?
protected $_filePointer; //according to php documentation, fsocken returns a file pointer. I need to be strict to properly understand.
public function __construct($hostname='ssl://mysocketpath.com', $porto=1231)
{
$this->_filePointer = fsockopen($hostname, $porto);
}
public function write($data, $length = null) //by doing this, we are telling $data is mandatory and $length is optional.
{
//writes the contents of $data to the file stream pointed by our file pointer, until length is reached.
//ternary operator: if $lenght is exactly equal to null, then, return the string length of $data, else, if (it's not NULL) return itself,
//this means, return the value it already have;
//WHY DO WE NEED THIS?
//TO TEST: IF I CHANGE TO $length===null WHAT WILL HAPPEN?
return fwrite($this->_filePointer, $data, null===$length ? strlen($data) : $length);
}
public function read($length)
{
return fread($this->_filePointer, $length);
}
}
EppFrameManager.Class
Note that I needed to change the sendFrame attribute:
class EppFrameManager
{
//const, because on all application, this value will never change. nice. :)
const OFFSET = 4;
//TO TEST: WHY PROTECTED and not PRIVATE - because we will use $_conexao on a descendent class?
protected $_conexao;
public function __construct(ConexaoEpp $conexao)
{
//pass the data received to a private property of our object
$this->_conexao = $conexao;
}
/**
*
* @param <string> $xml
*
* A frame, on this EPP context, is: the sum of a XML command instruction + 4 bytes containing the length.
* It will be that group that we call frame.
*
* So, I believe, sendFrame will receive a $xml and send a frame by using fwrite.
*/
public function sendFrame($xml)
{
//the (only?) way we have to access a const is by calling it staticly on self ? (no $this->OFFSET... ?)
return $this->_conexao->write(pack('N', strlen($xml)+self::OFFSET).$xml);
}
public function getFrame()
{
$unpacked = unpack('NLength', $this->_conexao->read(self::OFFSET));
//the reason why we are removing the FIRST (the length is at the beginning of our frame *) 4 bytes, is to be able to grab only the XML part of it,
//so, instead of returned right away, the solution I found was, place the remaining binary pack into
//a variable:
//* before, on the "old" code, it was the fact that fread was called twice that guarantee to us, that we are accessing the FIRST 4 bytes. What about now? :s
$binaryPack = $this->_conexao->read((int)$unpacked['lenght'] - self::OFFSET);
$xml = new SimpleXMLElement($binaryPack);
return $xml;
}
}
ComandosEppClass.php
require_once ('EppFrameManager.Class.php');
class ComandosEpp extends EppFrameManager
{
/**
*
* @return <object>
*/
public function hello()
{
//reads the XML send comands from the repository:
$xmlObj = simplexml_load_file('RepositorioXml/EppHello.xml');
//pass then into a string so that it can be consumed by sendFrame;
$xmlString = $xmlObj->asXML();
//sends the frame (conn->write->fwrite)
$this->sendFrame($xmlString);
//receives the frame containing the returned greeting comand:
$greetingXML = $this->getFrame();
//return the greeting command on a simpleXMLElement object.
return $greetingXML;
}
}
And finally:
EppHello.php
require_once('Classes/ComandosEpp.Class.php');
$comandos = new ComandosEpp;
$greeting = $comandos->hello();
$serverID = $greeting->svID;
echo $serverID;
When I run it, I’m getting:
Catchable fatal error: Argument 1 passed to EppFrameManager::__construct() must be an instance of ConexaoEppFccn, none given …
I would like to have it working, and then do some tests that I’ve marked and then, follow any suggestions you may have to make it better.
About the error:
Nothing as been passed to EppFrameManager construct.
So, nothing as been passed on EppFrameManager instantiation…
So, nothing as been passed on our Comandos class.
And it’s true, on Comandos (that extends EppFrameManager) nothing about connection is passed.
I thought that THAT could be the issue, so I put it, on ComandosEppClass the following:
public function __construct(ConexaoEpp $conexao)
{
parent::__construct($conexao);
}
But I guess it wrong. Same error occours…
We need to instantiate a connection object and pass it, I understand the error… but where?.. I’m missing some dots…
A little lost here… please have patience…
Márcio