Reading Zillow XML File with PHP - php

I have an XML file I want to read, but can't seem to make sense of it.
To access the file I pulled you can type in:
http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=X1-ZWz1gyo1562s5n_6sext&address=155+Demar+Blvd&citystatezip=Canonsburg%2C+PA
I'm doing this through PHP, so my code looks like:
<html>
<head>
<title>Hellow World</title>
</head>
<body>
<?php
$zillow_id = 'X1-ZWz1gyo1562s5n_6sext';
$search = '155 Demar Blvd';
$citystate = 'Canonsburg PA';
$address = urlencode($search);
$citystatezip = urlencode($citystate);
$url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=$zillow_id&address=$address&citystatezip=$citystatezip";
$result = file_get_contents($url);
$data = simplexml_load_string($result);
echo $data->response->results->result->lotSizeSqFt . "<br>";
?>
</body>
</html>
I had expected this code to result in the printout of the lot size - in square feet - to the screen. No dice.
However, the line of code:
echo $data->response->results->result->zpid . "<br>";
returns the expected value for the zpid parameter: 49785503.
In an ideal world the line of code:
echo $data->response->results->result->lotSizeSqFt . "<br>";
would return: 9000.
What am I doing wrong?

You're using the wrong endpoint in your code.
Your endpoint:
http://www.zillow.com/webservice/GetSearchResults.htm
The correct endpoint:
http://www.zillow.com/webservice/GetDeepSearchResults.htm
Using the GetDeepSearchResults will return the results you're looking for.

Related

Value NULL after program compile in browser

I'm making an application to remove additive or commonly called Stemming confix stripping. I wanted to make a loop to process stemming in each text file. process stemming I've put them in the loop. making the process the content of each document also I put in a text file. when I run in the browser no error but only the note NULL. what's the solution? I attach my program and program results`it is code
<?php
require_once __DIR__ . '/vendor/autoload.php';
$array_sentence = glob('../../ujicoba/simpantoken/*.txt');
settype($array_sentence, "string");
if(is_array($array_sentence) && is_object($array_sentence))
{
foreach ($array_sentence as $text)
{
$textnormalizer = new src\Sastrawi\Stemmer\Filter\TextNormalizer();
$stemmerFactory = new \Sastrawi\Stemmer\StemmerFactory();
$stemmer = $stemmerFactory->createStemmer();
$content = file_get_contents($text);
$stemmer = $stemmerFactory->createStemmer();
$output = $stemmer->stem(array($content));
echo $output . "\n";
}
}
var_dump($content);
?>
<!DOCTYPE html>
<html>
<head>
<title>Confix Stripping Stemmer</title>
</head>
<body>
</body>
</html>
my source code result in browser when running programenter code here
On l.4, settype($array_sentence, "string"); force $array_sentence as a string, which means is_array($array_sentence) && is_object($array_sentence) will return false.
The following code works for stemming:
<?php
include('stopword.php');
$regexRules = array(
'/^be(.*)lah$/',
'/^be(.*)an$/',
'/^me(.*)i$/',
'/^di(.*)i$/',
'/^pe(.*)i$/',
'/^ter(.*)i$/',
'/^di(.*)kan$/',
'/^di(.*)nya$/',
'/^di(.*)kannya$/',
'/^mem(.*)pe$/',
'/^meng(.*)g$/',
'/^meng(.*)h$/',
'/^meng(.*)q$/',
'/^meng(.*)k$/',
'/^mem(.*)kan$/',
'/^diper(.*)i$/',
'/^di(.*)i$/',
'/^memper(.*)kan$/',
'/^meny(.*)i$/',
'/^meny(.*)kan$/',
'/^men(.*)kan$/',
'/^me(.*)kan$/',
'/^meng(.*)nya$/',
'/^memper(.*)i$/',
'/^men(.*)i$/',
'/^meng(.*)i$/',
'/^ber(.*)nya$/',
'/^ber(.*)an$/',
'/^ke(.*)an$/',
'/^ke(.*)annya$/',
'/^peng(.*)an$/',
'/^peny(.*)an$/',
'/^per(.*)an$/',
'/^pen(.*)an$/',
'/^pe(.*)an$/',
'/^ber(.*)$/',
'/^di(.*)$/',
'/^men(.*)$/',
'/^meng(.*)$/',
'/^meny(.*)$/',
'/^mem(.*)$/',
'/^pen(.*)$/',
'/^peng(.*)$/',
'/^ter(.*)$/',
'/^mem(.*)$/',
'/^(.*)nya$/',
'/^(.*)lah$/',
'/^(.*)pun$/',
'/^(.*)kah$/',
'/^(.*)mu$/',
'/^(.*)an$/',
'/^(.*)kan$/',
'/^(.*)i$/',
'/^(.*)ku$/',
);
global $regexRules;
$file_string = glob('yourfoldertoseavedata_text/*.txt');
$string = array('(.*)');
foreach ($file_string as $data)
{
$string[] = file_get_contents($data);
$stemming = str_ireplace($regexRules,"", $string);
var_dump($stemming);
}
?>

PHP include HTML and echo out variable

I am working on a script with templates. So I have this PHP code:
<?php
$string = "TEST";
echo(file_get_contents('themes/default/test.html'));
?>
And I have this HTML (the test.html file):
<html>
<p>{$string}</p>
</html>
How can I make PHP actually display the variable inside the curly brackets? At the moment it displays {$string}.
P.S:
The string might also be an object with many many variables, and I will display them like that: {$object->variable}.
P.S 2: The HTML must stay as it is. This works:
$string = "I'm working!"
echo("The string is {$string}");
I need to use the same principle to display the value.
You can use the following code to achieve the desired result:
<?php
$string = "TEST";
$doc = file_get_contents('themes/default/test.html'));
echo preg_replace('/\{([A-Z]+)\}/', "$$1", $doc);
?>
P.S. Please note that it will assume that every string wrapped in { }
has a variable defined. So No error checking is implemented in the code above. furthermore it assumes that all variables have only alpha characters.
If it is possible to save your replacees in an array instead of normal variables you could use code below. I'm using it with a similar use case.
function loadFile($path) {
$vars = array();
$vars['string'] = "value";
$patterns = array_map("maskPattern", array_keys($vars));
$result = str_replace($patterns, $vars, file_get_contents($path));
return $result;
}
function maskPattern($value) {
return "{$" . $value . "}";
}
All you PHP must be in a <?php ?> block like this:
<html>
<p><?php echo "{" . $string . "}";?></p>
</html>
If you know the variable to replace in the html you can use the PHP function 'str_replace'. For your script,
$string = "TEST";
$content = file_get_contents('test.html');
$content = str_replace('{$string}', $string, $content);
echo($content);
It's simple to use echo.
<html>
<p>{<?php echo $string;?>}</p>
</html>
UPDATE 1:
After reading so many comments, found a solution, try this:
$string = "TEST";
$template = file_get_contents('themes/default/test.html', FILE_USE_INCLUDE_PATH);
$page = str_replace('{$string}',$string,$template);
echo $page;

How can I rewrite this PHP function to extract data from an external website/source

I have this PHP function that pulls/extracts data from xml elements and displays them on my webpage. However it is only working when used with a local path. not from any external sources. Here is the code.
Index.php
<html>
<body>
<?php
include('render_xml_to_html.php');
// The internal path. DOES work.
render_xml_data('example.xml');
?>
</body>
</html>
Example.xml
<eveapi version="2"><currentTime>2014-05-08 03:34:23</currentTime>
<result>
<serverOpen>True</serverOpen>
<onlinePlayers>24957</onlinePlayers>
</result>
<cachedUntil>2014-05-08 03:35:58</cachedUntil>
</eveapi>
The function - render_xml_to_html.php
<?php
function render_xml_data($path_to_xml_file){
if (!file_exists($path_to_xml_file)){
return;
}else{
$chars_to_replace = array('[\r]','[\n]','[\t]');
$xmlstring = trim(preg_replace($chars_to_replace, '', file_get_contents($path_to_xml_file)));
}
$xml = new SimpleXMLElement($xmlstring);
foreach ($xml->result as $record) {
echo '<div class="record">'."\n";
echo '<h3>'.$record->onlinePlayers.'</h3>'."\n";
echo '</div><!--end record-->'."\n";
}
}
?>
The above code works as is. My issue is when I try to pull this info from the realtime .xml file on the hosts server. That url is:
https://api.eveonline.com/server/ServerStatus.xml.aspx/
When I replace example.xml with the above link it fails to work. So the following doe not work. Where I link to an external path rather than a local.
<?php
include('render_xml_to_html.php');
// The external path DOES NOT WORK
render_xml_data('https://api.eveonline.com/server/ServerStatus.xml.aspx/');
?>
Thanks in advance!
You can just use file_get_contents on this one, it can get the job done. Consider this example:
$url = 'https://api.eveonline.com/server/ServerStatus.xml.aspx/';
// access the url and get that file
$contents = file_get_contents($url);
// convert it to an xml object
$contents = simplexml_load_string($contents);
echo "<div class='record'>Number of Online Players: ".$contents->result->onlinePlayers."</div>";
Sample Output:
Number of Online Players: 23893

Pulling wiki to html using php and javascript

I would like to change wiki syntax to html using these techniques mentioned here.
http://www.ladyada.net/library/software/wiki.html
Here is the source code of my php page which seems to work fine. You may try yourself like this:
http://temelelektronik.net/wiki/show.php?id=test
<?
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/common.php');
require_once(DOKU_INC.'inc/events.php');
require_once(DOKU_INC.'inc/pageutils.php');
require_once(DOKU_INC.'inc/html.php');
require_once(DOKU_INC.'inc/auth.php');
require_once(DOKU_INC.'inc/actions.php');
//import variables
$QUERY = trim($_REQUEST['id']);
$ID = getID();
$REV = $_REQUEST['rev'];
$HIGH = $_REQUEST['s'];
if(empty($HIGH)) $HIGH = getGoogleQuery();
//sanitize revision
$REV = preg_replace('/[^0-9]/','',$REV);
$html = p_wiki_xhtml($ID,$REV,true);
$html = addslashes($html);
$html = ereg_replace("\n", '\n'."\\\n", $html);
$html = str_replace("</a>", "</a> ", $html);
echo ('var zoomy = "'.$html.'";');
echo ("document.write(zoomy);");
$url = "http://www.temelelektronik.net/wiki/" . str_replace(':', '/', $_REQUEST['id']);
$footer = '<hr /><br /><em>This page was autogenerated from
' . $url . ' <br />Please edit the wiki to contribute any updates or corrections.</em>';
$footer = '"' . $footer . '"';
echo ('document.write('. $footer . ')');
?>
But when I add this Javascript code to a blank html page I see nothing. Here is the HTML file.
<html>
<head>
<script language="Javascript" src="/show.php?id=test"></script>
</head>
<body>
</body>
</html>
As you'd see it shows nothing.
http://temelelektronik.net/wiki/test.html
How can I solve this issue? Any help you can give will be greatly appreciated. Thanks..
Because you supplied the wrong URL?
http://temelelektronik.net/wiki/show.php?id=test
vs.
<script language="Javascript" src="/show.php?id=test"></script>
Apparently it should be /wiki/show.php?id=test

Url and title with php snippet

I read several similar posts but I don't see my fault.
index.php looks like:
<head>
<title>Demo Title</title>
</head>
<body>
<?php
require_once "footer.php";
?>
</body>
footer.php looks like:
<?php
/*
* _$ Rev. : 08 Sep 2010 14:52:26 $_
* footer.php
*/
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo $url;
$file = # fopen($_SERVER[$url],"r") or die ("Can't open HTTP_REFERER.");
$text = fread($file,16384);
if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) {
$title = $found[1];
} else {
$title = " -- no title found -- ";
}
?>
A request for the URL http://127.0.0.1/test/index.php results in:
http://127.0.0.1/test/index.phpCan't open HTTP_REFERER.
or for http://127.0.0.1/test/
http://127.0.0.1/test/Can't open HTTP_REFERER.
Any hints appreciated.
$_SERVER is an array which contains a bunch of fields relating to the server config. It does not contain an element named "http://".$host.$param, so trying to open that as a filename will result in the fopen call failing, and thus going to the die() statement.
More likely what you wanted to do was just open the file called "http://".$host.$param. If that's what you want, then just drop the $_SERVER[] bit and it should work better.
Note that because it's a URL, you will need your PHP config to allow opening of remote files using fopen(). PHP isn't always configured this way by default as it can be a security risk. Your dev machine may also be configured differently to the system you will eventually deploy to. If you find you can't open a remote URL using fopen(), there are alternatives such as using CURL, but they're not quite as straightforward as a simple fopen() call.
Also, if you're reading the whole file, you may want to consider file_get_contents() rather than fopen() and fread(), as it replaces the whole thing into a single function call.
try this:
$file = # fopen($url,"r") or die ("Can't open HTTP_REFERER.");
Try
<?php
$dom = new DOMDocument();
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo 'getting title for page: "' . $url . '"';
$dom->loadHTML($url);
$dom->getElementsByTagName('title');
if ($dom->length)
{
$title = $dom->item(0);
echo $title;
}
else
{
echo 'title tag not found';
}
?>
I can see your trying to track the referral's title
You need to use $_SERVER['HTTP_REFERER']; to get that
what you want to do is something like this
$referrer = (!empty($_SERVER['HTTP_REFERER']) && !substr($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_REFERER'] : false);
if($referrer)
{
try
{
if(false !== ($resource = fopen($referrer,"r")))
{
$contents = '';
while($contents .= fread($resource,128))
{}
if(preg_match('/<title>(.*?)<\/title>/is',$contents,$found))
{
echo 'Referrer: ' $found[1];
}
}
}catch(Exception $e){}
}

Categories