Last active
August 29, 2015 14:08
-
-
Save BillKeenan/6a2cb1567a43aba72e3c to your computer and use it in GitHub Desktop.
cottages for james
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: bill | |
* Date: 14-11-06 | |
* Time: 2:21 PM | |
*/ | |
error_reporting(E_ALL); | |
//utiltiy class for making the actual calls. | |
class apiBridge { | |
private $protocol = "ssl"; | |
private $host ="agent.barefoot.com"; | |
private $feedBase = '/BarefootWebService/BarefootService.asmx'; | |
public $listingsMethod ='GetProperty'; | |
private $imagesMethod='GetPropertyAllImgsXML'; | |
public $err; | |
private $fp; | |
private function connect(){ | |
$this->fp = fsockopen(sprintf("%s://%s",$this->protocol,$this->host), 443, $errno, $errstr, 30); | |
if (!$this->fp) { | |
$err= "$errstr ($errno)"; | |
return false; | |
} | |
return true; | |
} | |
private function getRequest($method,$body){ | |
$header = sprintf( | |
"POST /BarefootWebService/BarefootService.asmx HTTP/1.1\r\n". | |
"Host: agent.barefoot.com\r\n". | |
"Connection: Keep-Alive\r\n". | |
"User-Agent: PHP-SOAP/5.3.2-1ubuntu4.24\r\n". | |
"Content-Type: text/xml; charset=utf-8\r\n". | |
"SOAPAction: \"http://www.barefoot.com/Services/%s\"\r\n". | |
"Content-Length: %s\r\n\r\n%s" | |
,$method,strlen($body),$body); | |
return $header; | |
} | |
public function getData($method,$request){ | |
if ($this->connect() == false){ | |
throw new Exception($this->err); | |
} | |
$out = $this->getRequest($method ,$request); | |
$result = ""; | |
fwrite($this->fp, $out); | |
$inBody = false; | |
$length=0; | |
while (!feof($this->fp)) { | |
$line = fgets($this->fp); | |
if(strpos($line,'Content-Length') === 0){ | |
$length = intval(substr($line,strpos($line,':') +2)); | |
} | |
if($inBody==false && $line=="\r\n"){ | |
$inBody=true; | |
//dont append this line break | |
continue; | |
} | |
if ($inBody == false){ | |
continue; | |
} | |
$result.=$line; | |
if (strlen($result) >= (int)$length){ | |
break; | |
} | |
} | |
return $result; | |
} | |
public function close(){ | |
if ($this->fp != null){ | |
fclose($this->fp); | |
} | |
} | |
public function getOurContent($method,$xml){ | |
$matches =null; | |
$regex = sprintf('/.*?%sResult>(.*)<\/%sResult.*/s',$method,$method); | |
preg_match_all($regex,$xml,$matches); | |
//should be 2 | |
if (count($matches) != 2){ | |
throw new Exception('cant find result'); | |
} | |
// print_r($matches[1][0]); | |
// exit; | |
$ourXml = html_entity_decode($matches[1][0]); | |
$listingsWrapper = new DOMDocument(); | |
$listingsWrapper->loadXML($ourXml); | |
return $listingsWrapper; | |
} | |
} | |
function microtime_float() | |
{ | |
list($usec, $sec) = explode(" ", microtime()); | |
return ((float)$usec + (float)$sec); | |
} | |
$time_start = microtime_float(); | |
$user = 'v3ccv0106'; | |
$pass = '#v3c0106'; | |
$account='v3ccv0106'; | |
$api = new apiBridge(); | |
$getAllListings = @"<?xml version='1.0' encoding='UTF-8'?> | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://www.barefoot.com/Services/'> | |
<SOAP-ENV:Body> | |
<ns1:GetProperty> | |
<ns1:username>%s</ns1:username> | |
<ns1:password>%s</ns1:password> | |
<ns1:barefootAccount>%s</ns1:barefootAccount> | |
</ns1:GetProperty> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope>"; | |
$getAllImages = @"<?xml version='1.0' encoding='utf-8'?> | |
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> | |
<soap:Body> | |
<GetPropertyAllImgsXML xmlns='http://www.barefoot.com/Services/'> | |
<username>%s</username> | |
<password>%s</password> | |
<barefootAccount>%s</barefootAccount> | |
<propertyId>%s</propertyId> | |
</GetPropertyAllImgsXML> | |
</soap:Body> | |
</soap:Envelope>"; | |
$xml = $api->getData('GetProperty',sprintf($getAllListings,$user,$pass,$account)); | |
//use this one for debugging | |
//$xml = file_get_contents('allListings.xml'); | |
//lets just pull this out | |
$ourXml = $api->getOurContent('GetProperty',$xml); | |
$xpath = new DOMXpath($ourXml); | |
//get all the properties | |
$elements = $xpath->query("/PropertyList/Property"); | |
$listings = Array(); | |
$properties=Array(); | |
//all the properties we need. add whatever you need here. | |
array_push($properties,'PropertyID','extdescription'); | |
//build up our listings | |
foreach ($elements as $node){ | |
$listing = Array(); | |
foreach($properties as $prop){ | |
$listing[$prop] = htmlentities($node->getElementsByTagName($prop)->item(0)->textContent,ENT_QUOTES, "UTF-8"); | |
} | |
array_push($listings,$listing); | |
} | |
//now we have all our listings | |
$xmlObject = new SimpleXMLElement('<Properties/>'); | |
//step through them, add images | |
foreach ($listings as &$listing){ | |
$thisXMlListing = $xmlObject->addChild('Property'); | |
foreach($properties as $prop){ | |
$thisXMlListing->addChild($prop); | |
$thisXMlListing->$prop = $listing[$prop]; | |
} | |
$xml = $api->getData('GetPropertyAllImgsXML',sprintf($getAllImages,$user,$pass,$account,$listing['PropertyID'])); | |
//use this one for debuggin | |
//$xml = file_get_contents('allImages.xml'); | |
//lets just pull this out | |
$ourXml = $api->getOurContent('GetPropertyAllImgsXML',$xml); | |
$xpath = new DOMXpath($ourXml); | |
//just the image please | |
$elements = $xpath->query("/Property/PropertyImg/imagepath"); | |
$imageNode = $thisXMlListing->addChild('images'); | |
foreach ($elements as $node){ | |
$imageNode->addChild('image',$node->nodeValue); | |
} | |
} | |
header ("Content-Type:text/xml"); | |
echo $xmlObject->asXML(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment