getting empty response from RETS server using PHrets - rets

I got connected to RETS Server and i am able to get all metadata using functions in PHrets library like GetMetadataResources,GetMetadataClasses,and all feilds in each class,but i am unable to get data from that server using SearchQuery,I tried in all ways but no luck.
<pre>
/* Initialize Object */
require("PHRets_CREA.php");
require("credentials.php");
$rets = new PHRets();
$connect=$rets->Connect($RETSURL, $RETSUsername, $RETSPassword);
/* Connect */
/* Query Server */
if ($connect) {
echo "Connected";
$resources = $rets->GetMetadataResources();
foreach ($resources as $resource) {
echo "+ Resource {$resource['ResourceID']}\n";
$classes = $rets->GetMetadataClasses($resource['ResourceID']);
foreach ($classes as $class) {
echo " + Class {$class['ClassName']} described as " . $class['Description'] . "\n";
$rets_metadata = $rets->GetMetadata($resource['ResourceID'],$class['ClassName']);
foreach ($rets_metadata as $field) {
echo " + Field: {$field['SystemName']} ({$field['DataType']})\n";
}
}
}
/*
$search = $rets->SearchQuery("Property","RE_1","(L_ListingDate=2010-03-02-2010-03-03)");
while ($listing = $rets->FetchRow($search)) {
echo $listing['L_ListingID'];
}*/
$search = $rets->SearchQuery("Property","ResidentialProperty","(ListDate=1990-01-01+)",
array( "Count" => 1, "Format" => "COMPACT", "Limit" => "2", "Offset" => "2",
"RestrictedIndicator" => "****", "StandardNames" => 1 )
);
echo "data";
print_r($search);
while ($listing = $rets->FetchRow($search))
echo "{$listing['L_City']}";exit;
}
$rets->FreeResult($search);
}</pre>
Thanks

First make sure your Resource and Class name are correct. Use http://retsmd.com to verify. Resource is usually "Property" but check your Class name which you currently have to "ResidentialProperty". It may be "RE_1" which you had in your above code.
Next, modify your SearchQuery. Maybe use less parameters in the options array and try removing StandardNames and use the default System Names.
Something like this:
$search = $rets->SearchQuery("Property","RE_1","(L_ListingDate=1990-01-01+)", array("Limit" => "10"));

Please try this one, we get all active properties for this.
$search = $rets->SearchQuery("Property","1","(135=1990-01-01+),(246=A)");

Related

Bing API related searches

How does one get related searches to be included in response from Bing search API?
I am trying to apply responseFilter with value RelatedSearches as per the documentation here: https://learn.microsoft.com/en-us/rest/api/cognitiveservices/bing-web-api-v7-reference#relatedsearchanswer
Below is my code based on sample from Bing. I have added "&responseFilter=RelatedSearches" to the URL that is fetched by file_get_contents().
The error I get back is:
PHP Warning: file_get_contents(https://api.cognitive.microsoft.com/bing/v7.0/search?responseFilter=RelatedSearches&q=iphone+case): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Clearly I am applying it wrong. What can I change to be making it working?
<?php
$accessKey = 'abc123';
$endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/search';
$term = 'burrito recipe';
function BingWebSearch ($url, $key, $query) {
$headers = "Ocp-Apim-Subscription-Key: $key\r\n";
$options = array ('http' => array (
'header' => $headers,
'method' => 'GET'));
$context = stream_context_create($options);
$result = file_get_contents($url . "?q=" . urlencode($query) . "&responseFilter=RelatedSearches", false, $context);
$headers = array();
foreach ($http_response_header as $k => $v) {
$h = explode(":", $v, 2);
if (isset($h[1]))
if (preg_match("/^BingAPIs-/", $h[0]) || preg_match("/^X-MSEdge-/", $h[0]))
$headers[trim($h[0])] = trim($h[1]);
}
return array($headers, $result);
}
if (strlen($accessKey) == 32) {
print "Searching the Web for: " . $term . "\n";
list($headers, $json) = BingWebSearch($endpoint, $accessKey, $term);
print "\nRelevant Headers:\n\n";
foreach ($headers as $k => $v) {
print $k . ": " . $v . "\n";
}
print "\nJSON Response:\n\n";
echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
} else {
print("Invalid Bing Search API subscription key!\n");
print("Please paste yours into the source code.\n");
}
?>
The "403 Forbidden" is the hint. You don't have permission to request RelatedSearches unless you are on the S1 instance as per here: https://azure.microsoft.com/en-us/pricing/details/cognitive-services/search-api/

Connecting with multiple resources in PHRETS

I have to retrieve the data from multiple resources by using the Rets server. Is there any way to do so?
For example,
I have resources and classes like:
Property(Resource), Listing(Class)
Media(Resource), Media(Class)
Some data should be fetched from the first and a little bit from the second.
I am using the Phrets to retrieve the data from the Rets server.
I am using the source code:
<?php
date_default_timezone_set('America/New_York');
require_once("vendor/autoload.php");
$log = new \Monolog\Logger('PHRETS');
$log->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));
$config = new \PHRETS\Configuration;
$config->setLoginUrl('Url to connect with')
->setUsername('Username to login')
->setPassword('Password to login');
->setRetsVersion('1.5')
$rets = new \PHRETS\Session($config);
$rets->setLogger($log);
$connect = $rets->Login();
$resource = 'Property';
$class = 'Listing';
$query = "(Acres=0+)";
$options = array(
'Count' => 1,
'Format' => 'COMPACT-DECODED',
'Limit' => 50,
'StandardNames' => 0,
'Select'=>'Acres,City,ClosePrice,BathsFull,BathsHalf,PhotoCount,VirtualTourLink'
);
$results = $rets->Search($resource,$class,$query,$options);
foreach($results as $record){
echo "<table>";
echo "<tr>";
echo "<td>".$record['Acres']."</td>&nbsp";
echo "<td>".$record['City']."</td>";
echo "<td>".$record['ClosePrice']."</td>";
echo "<td>".$record['BathsFull']."</td>";
echo "<td>".$record['BathsHalf']."</td>";
echo "<td>".$record['VirtualTourLink']."</td>";
echo "</tr>";
echo "</table>";
}
?>
Thanks in advance!
Fetch your records from the first class and then fetch your records from the second class using the second classes dependent id. Media classes are always dependent on the property classes, so it will have some id that corresponds to the main id of the property class (usually the mls # of the listing). Without seeing your RETS server's metadata I won't be able to tell you the names of the fields you need to pull from. Also, your version is more than likely 1.7.2, AFAIK no one really uses 1.5 anymore.
You can fetch by using loop of class name.
$resource = 'Property';
$classes = array('Listing','Media');
$query = "(Acres=0+)";
$options = array(
'Count' => 1,
'Format' => 'COMPACT-DECODED',
'Limit' => 50,
'StandardNames' => 0,
'Select'=>'Acres,City,ClosePrice,BathsFull,BathsHalf,PhotoCount,VirtualTourLink'
);
foreach($classes as $class){
$results[$class] = $rets->Search($resource,$class,$query,$options);
}

Concrete5.7.5.2 - How to get Topics tree attributes values into a select box?

I have a Topics tree which I use as an attribute on a page:
Categories-
-Topic 1
-Topic 2
-Topic 3
How can I get the topics into an array in a block? Which I can then use in a select box? e.g.
$topics = ("Topic 1", "Topic 2", "Topic 3);
echo $form->select('categories', $topics);
And if my select box is on the right most side of the page, it always has the right border missing. If I move it anywhere else, it shows fine. Anyone else have this?
BTW, for those who want to get values from a select box attribute:
use Concrete\Core\Attribute\Key\CollectionKey as CollectionKey;
use Concrete\Attribute\Select\Controller as SelectController;
use Concrete\Core\Attribute\Type as AttributeType;
$ak = CollectionKey::getByHandle('region');
$at = AttributeType::getByHandle('select');
$satc = new SelectController($at);
$satc->setAttributeKey($ak);
$values = $satc->getOptions()->getOptions();
foreach ($values as $key => $value) {
$this->options[$value->getSelectAttributeOptionID()] = $value->getSelectAttributeOptionValue();
}
Thank you.
[SOLVED]
Thanks to Mike, here's a piece of working code:
use Concrete\Core\Tree\Type\Topic as TopicTree;
public $category = array('');
public function view() {
...
$this->requireAsset('core/topics');
$tt = new TopicTree();
$tree = $tt->getByName('My Categories');
$node = $tree->getRootTreeNodeObject();
$node->populateChildren();
if (is_object($node)) {
foreach($node->getChildNodes() as $key => $category) {
if ($category instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$this->category[$category->getTreeNodeDisplayName()] = $category->getTreeNodeDisplayName();
}
}
}
...
}
You could do something like this within your block controller...
private function getTopics($topicTreeName)
{
$this->requireAsset('core/topics');
$tt = new TopicTree();
/** #var Topic $tree */
$tree = $tt->getByName($topicTreeName);
/** #var TopicCategory $node */
$node = $tree->getRootTreeNodeObject();
$node->populateChildren();
$topics = [];
/** #var Concrete/Core/Tree/Node/Type/Topic $topic */
foreach ($node->getChildNodes() as $topic) {
if ($topic instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$topics[] = [
'id' => $topic->getTreeNodeID(),
'name' => $topic->getTreeNodeDisplayName(),
];
}
}
return $topics;
}
This will get you an array of topics and their Id's (I suspect you would want the id for the value of the select option) like this...
[[name=>'Topic 1', id => 1], [name=>'Topic 2', id => 2]..etc.]
...and then within your view function you can set the variable to make it available within the view template...
public function view() {
$topics = $this->getTopics('My topic name');
$this->set('topics', $topics);
}
You could just then iterate over the topics within your template to output the select list.
Hope that helps with how to get the list of topics?

Laravel how to get query with bindings?

I have some query that I need to pass to another query using query builder
$query = DB::table('table')->whereIn('some_field', [1,2,30])->toSql();
Model::join(DB::raw("({$query}) as table"), function($join) {
$join->on('model.id', '=', 'table.id');
})
which should results with
Select * from model join (select * from table where some_field in (1,2,30)) as table on model.id = table.id
but the bindings are not passed, which force me to do
$query = DB::table('table')->whereRaw('some_field in ('. join(',', [1,2,30]) .')')->toSql();
what can be unsafe at times. How can I get the query with bindings?
Check out the getBindings() method on the Builder class
getBindings()
$query = DB::table('table')->whereIn('some_field', [1,2,30]);
$sql = $query->toSql();
$bindings = $query->getBindings();
Laravel now offers debugging directly on your Builder!!!
https://laravel.com/docs/queries#debugging
\App\User::where('age', '18')->dump();
\App\User::where('age', '18')->dd();
Outputs
"select * from `users` where `age` = ?"
[
0 => "18"
]
public static function getQueries(Builder $builder)
{
$addSlashes = str_replace('?', "'?'", $builder->toSql());
return vsprintf(str_replace('?', '%s', $addSlashes), $builder->getBindings());
}
You can define below code block as helper function and use wherever required.
It will bind numeric as well as string value with quotations.
public static function getSqlWithBindings($query)
{
return vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(function ($binding) {
return is_numeric($binding) ? $binding : "'{$binding}'";
})->toArray());
}
Example:
$query = Document::where('model', 'contact')->where('model_id', '1');
dd(Document::getSqlWithBindings($query));
Output:
"select * from `document` where `model` = 'contact' and `model_id` = 1"
Building upon Douglas.Sesar's answer.
I found I also needed to put the bindings in single quotations to be able to easily paste it into my database IDE.
$sql = $query->toSql();
$bindings = $query->getBindings();
$sql_with_bindings = preg_replace_callback('/\?/', function ($match) use ($sql, &$bindings) {
return json_encode(array_shift($bindings));
}, $sql);
$sqlQuery = Str::replaceArray(
'?',
collect($query->getBindings())
->map(function ($i) {
if (is_object($i)) {
$i = (string)$i;
}
return (is_string($i)) ? "'$i'" : $i;
})->all(),
$query->toSql());
The following function ensures the resulting SQL doesn't confuse bindings with columns by enclosing the ? to be '?'
public static function getFinalSql($query)
{
$sql_str = $query->toSql();
$bindings = $query->getBindings();
$wrapped_str = str_replace('?', "'?'", $sql_str);
return str_replace_array('?', $bindings, $wrapped_str);
}
If you want to get an executed query including bindings from the query log:
\DB::enableQueryLog();
\DB::table('table')->get();
dd(str_replace_array('?', \DB::getQueryLog()[0]['bindings'],
\DB::getQueryLog()[0]['query']));
I created this function. It is partial, might be parameters which are not covered, for me it was enough.
More than welcomed to add your improvements in a comment!
function getFullSql($query) {
$sqlStr = $query->toSql();
foreach ($query->getBindings() as $iter=>$binding) {
$type = gettype($binding);
switch ($type) {
case "integer":
case "double":
$bindingStr = "$binding";
break;
case "string":
$bindingStr = "'$binding'";
break;
case "object":
$class = get_class($binding);
switch ($class) {
case "DateTime":
$bindingStr = "'" . $binding->format('Y-m-d H:i:s') . "'";
break;
default:
throw new \Exception("Unexpected binding argument class ($class)");
}
break;
default:
throw new \Exception("Unexpected binding argument type ($type)");
}
$currentPos = strpos($sqlStr, '?');
if ($currentPos === false) {
throw new \Exception("Cannot find binding location in Sql String for bundung parameter $binding ($iter)");
}
$sqlStr = substr($sqlStr, 0, $currentPos) . $bindingStr . substr($sqlStr, $currentPos + 1);
}
$search = ["select", "distinct", "from", "where", "and", "order by", "asc", "desc", "inner join", "join"];
$replace = ["SELECT", "DISTINCT", "\n FROM", "\n WHERE", "\n AND", "\n ORDER BY", "ASC", "DESC", "\n INNER JOIN", "\n JOIN"];
$sqlStr = str_replace($search, $replace, $sqlStr);
return $sqlStr;
}
You can do something like this:
$escapedBindings = array();
foreach($query->getBindings() as $item) {$escapedBindings[] = '"'.$item.'"';}
$sql_with_bindings = Str::replaceArray('?', $escapedBindings, $query->toSql());
This is a very old question (2015), but since this is the first Google result I got I think it's worth to give my solution as well, in case it's useful for the next person.
Eloquent (5.7 onwards I think, I haven't tested more recent or earlier versions) has a method to change a Builder's from to wrap a subquery:
# Taken from Illuminate/Database/Query/Builder.php - Line 272
public function fromSub($query, $as) {
[$query, $bindings] = $this->createSub($query);
return $this->fromRaw('('.$query.') as '.$this->grammar->wrapTable($as), $bindings);
}
This however requires an already existing instance of \Illuminate\Database\Query\Builder. In order to make an empty one, you can do:
use Illuminate\Database\Capsule\Manager as DB;
$fancy = DB::table("videogames")->where("uses_sdl2", 1);
$empty = DB::table(null);
# Wrap the fancy query and set it as the "from" clause for the empty one
# NOTE: the alias is required
$empty = $empty->fromSub($fancy, "performant_games");
This will warranty that bindings are treated correctly, since they'll be handled by Eloquent itself.
Since the other answers do not properly quote the expressions, here is my approach. It uses the escaping function that belongs to the current database connection.
It replaces the question marks one by one with the corresponding binding, which is retrieved from $bindings via array_shift(), consuming the array in the process. Note, that $bindings has to be passed by reference for this to work.
function getSql($query)
{
$bindings = $query->getBindings();
return preg_replace_callback('/\?/', function ($match) use (&$bindings, $query) {
return $query->getConnection()->getPdo()->quote(array_shift($bindings));
}, $query->toSql());
}
Simple and elegant solution:
foreach (DB::getQueryLog() as $q) {
$queryStr = \Str::replaceArray('?', $q['bindings'], $q['query']);
echo $queryStr . ";\n";
}
(if you use a non-default connection, use DB::connection('yourConn')->getQueryLog() in the foreach command).
Output to the log all queries with inserted bindings sorted from the slowest query to the fastest:
\DB::enableQueryLog();
// Put here your queries
$query = DB::table('table')->whereIn('some_field', [1,2,30]);
$query2 = DB::table('table2')->where('some_field', '=', 10);
$logQueries = \DB::getQueryLog();
usort($logQueries, function($a, $b) {
return $b['time'] <=> $a['time'];
});
foreach ($logQueries as $item) {
\Log::info(str_replace_array('?', $item['bindings'], $item['query']));
\Log::info($item['time']. ' ms');
}
It is all explained here.....
https://ajcastro29.blogspot.com/2017/11/laravel-join-derived-tables-properly.html
I created a scope query for that thing. I think it can also be in macros..
public function scopeJoinDerived($query, $derivedQuery, $table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
{
$query->join(DB::raw("({$derivedQuery->toSql()}) as `{$table}`"), $one, $operator, $two, $type, $where);
$join = last($query->getQuery()->joins);
$join->bindings = array_merge($derivedQuery->getBindings(), $join->bindings);
return $query;
}

Incorrect Signature in facebook stream_publish call

I am having a facebook error on stream_publish call. I actually used an extension for Magento for Fconnect. Fconnect & Flogin is working fine. But it is requirement that when user place an order it should be posted on user's wall. For that I have implemented like this
document.observe('click', function(e){
if (e.element().match('a[rel^=facebook-connect]') || e.element().match('button[rel^=facebook-connect]')) {
e.stop();
FB.login(function(response){
if(response.status=='connected') setLocation('http://staging.mystore.com/facebook/customer_account/connect/');
}, {perms:"email,publish_stream"});
}
});
in Facebook Client file generateSignature method is like this
private function _generateSig($params_array)
{
Mage::log($params_array);
$str = '';
ksort($params_array);
foreach ($params_array as $k=>$v) {
$str .= "$k=$v";
}
$str .= $this->_secret;
Mage::log($str);
Mage::log('md5 sigs:: ' . md5($str));
return md5($str);
}
& My code that is calling the API is like this
$message = 'just placed an order on mystore.com';
$attachment = array(
'name' => "mystore",
'href' => 'http://www.mystore.com/',
'description' => 'New order on mystore.com',
'media' => array(array('type' => 'image',
'src' => 'http://www.mystore.com/skin/frontend/default/mystore/images/logo.png',
'href' => 'http://www.mystore.com/')));
$action_links = array( array('text' => 'Buy#mystore', 'href' => 'http://www.mystore.com/'));
$attachment = json_encode($attachment);
$action_links = json_encode($action_links);
try{
// if( $facebook->api_client->stream_publish($message, $attachment, $action_links, null, $target_id))
if($this->_getClient()->call( 'facebook.stream.publish',
array($message, $attachment, $action_links,
$this->_getClient()->users->getLoggedInUser(),
Mage::getSingleton('facebook/config')->getApiKey() )
) )
{
Mage::log( "Added on FB Wall" );
}
} catch(Exception $e)
{
Mage::log( "Exception in wall write" );
Mage::log($e);
}
After logging the Signature I found in log is
api_key=XXXXXXXXmethod=facebook.stream.publishsession_key=2.AQCm5fABfobInAS5.3600.1309352400.1-1000025660978090=just placed an order on mystore.comcall_id=1309345883.3068format=JSONv=1.01={"name":"mystore","href":"http:\/\/www.mystore.com\/","description":"New order on mystore.com","media":[{"type":"image","src":"http:\/\/www.mystore.com\/skin\/frontend\/default\/mystore\/images\/logo.png","href":"http:\/\/www.mystore.com\/"}]}2=[{"text":"Buy#mystore","href":"http:\/\/www.mystore.com\/"}]3=1000025660978094=5070afefb42b162aff748f55ecf44d110d9e2a90117ee1704e2adb41f1d190fa
I have never done any development on Facebook SO I have no Idea what to do? Please help me with solution. & let me know if u guys need any other info to understand this.
Oh yeah One more thing the Client File code that is calling Api (call method) its like this
private function _prepareParams($method, $params)
{
$defaultParams = array(
'api_key' => $this->_apiKey,
'call_id' => microtime(true),
'format' => 'JSON',
'v' => '1.0'
);
if($this->_sessionKey){
$defaultParams['session_key'] = $this->_sessionKey;
}
$params = array_merge($defaultParams, $params);
foreach ($params as $key => &$val) {
if (!is_array($val)) continue;
$val = Zend_Json::encode($val);
}
$params['method'] = $method;
if(isset($params['sig'])) {
unset($params['sig']);
}
$params['sig'] = $this->_generateSig($params);
return $params;
}
public function call($method, $args=array())
{
Mage::log($args);
$params = $this->_prepareParams($method, $args);
$client = self::_getHttpClient()
->setUri(self::FACEBOOK_REST_URI)
->setMethod(Zend_Http_Client::POST)
->resetParameters()
->setParameterPost($params);
try {
$response = $client->request();
} catch(Exception $e) {
throw new Mage_Core_Exception('Service unavaliable');
}
if(!$response->isSuccessful()) {
throw new Mage_Core_Exception('Service unavaliable');
}
$result = Zend_Json::decode($response->getBody());
//json decode returns float on long uid number? is_json check? old php?
if(is_float($result)){
$result = $response->getBody();
}
if(is_array($result) && isset($result['error_code'])) {
throw new Mage_Core_Exception($result['error_msg'], $result['error_code']);
}
return $result;
}
For calling API I used two ways $this->_getClient()->call( 'facebook.stream.publish',
& $this->_getClient()->call( 'stream_publish',
None of them are working
ok GUys I figure out the mistake
look at my code
format=JSONv=1.01={"name":"mystore","href":"http:\/\/www.mystore.com\/","description":"New order on mystore.com","media":[{"type":"image","src":"http:\/\/www.mystore.com\/skin\/frontend\/default\/mystore\/images\/logo.png","href":"http:\/\/www.mystore.com\/"}]}2=[{"text":"Buy#mystore","href":"http:\/\/www.mystore.com\/"}]3=1000025660978094=5070afefb42b162aff748f55ecf44d110d9e2a90117ee1704e2adb41f1d190fa
where u can see format=JSONv=1.01={....}2=[{.....}] the problem was I used numeric arrays for parameters. they should be associated arrays
like message={new order}attachment={....}
Once I fixed the associative array problem my code start working correctly
here is a link that'll give u detail about parameters to pass to stream.publish
http://schoolout.net/en/developers/view/39
Hope this will help somebody else too.

Resources