PHP MongoDB inconsistent results with query on MongoDate - php

I'm running a very basic script on a sharded MongoDb setup to select the number of messages on a given day.
However, running this multiple times, results in inconsistent results, sometimes it returns the number, sometimes it just returns 0.
The mongodb server has version 2.4.3
Am I missing something here?
This is the script:
<?php
$mongo = new MongoClient("mongodb://127.0.0.1:27017");
$db = $mongo->selectDB('database');
$messages = $db->selectCollection('messages');
$date1 = new MongoDate(strtotime('2013-10-20'));
$date2 = new MongoDate(strtotime('2013-10-21'));
var_dump($date1);
var_dump($date2);
$iterator = $messages->find(array('date_replied' => array('$gte' => $date1, '$lt' => $date2)));
$count = $iterator->count();
var_dump($count);
And this is the result, 2 times ran directly after each other:
~$ php -f mongo.php
object(MongoDate)#4 (2) {
["sec"]=>
int(1382220000)
["usec"]=>
int(0)
}
object(MongoDate)#5 (2) {
["sec"]=>
int(1382306400)
["usec"]=>
int(0)
}
int(494921)
~$ php -f mongo.php
object(MongoDate)#4 (2) {
["sec"]=>
int(1382220000)
["usec"]=>
int(0)
}
object(MongoDate)#5 (2) {
["sec"]=>
int(1382306400)
["usec"]=>
int(0)
}
int(0)

Your database might be corrupt. Can you do a db.repairDatabase() and try again? It this fails try to re-create the index, I had similar issues with distributed collections.
Please keep in mind that repairing will block the whole database operations and might throw away any corrupted data. Proceed with care!

Related

mongoDB php doesn't find database but is successfully connected to the server

when I connect to my mongoDB database with PHP, the variable sometimes contains an empty array but when pinging it, the array contains data. The empty arraay appears when I just restarted the apache2 webserver. I guess there are some other ways to cause it but I can't find a way on how to do that.
This is not my only issue. When I select a database, the variable contains NULL so I can't select a collection. It's also very hard to find an up to date documantation because these two are outdated.
Now to all my code:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
var_dump($mongo);
sometimes returns:
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(47) "mongodb://root:pwd#localhost:27017" ["cluster"]=> array(0) { } }
Sending a ping to the database before printing the database:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
var_dump($mongo);
returns:
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(47) "mongodb://root:pwd#localhost:27017" ["cluster"]=> array(1) { [0]=> array(10) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(1) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["last_is_master"]=> array(10) { ["ismaster"]=> bool(true) ["maxBsonObjectSize"]=> int(16777216) ["maxMessageSizeBytes"]=> int(48000000) ["maxWriteBatchSize"]=> int(100000) ["localTime"]=> object(MongoDB\BSON\UTCDateTime)#2 (1) { ["milliseconds"]=> string(13) "1618843900377" } ["logicalSessionTimeoutMinutes"]=> int(30) ["minWireVersion"]=> int(0) ["maxWireVersion"]=> int(6) ["readOnly"]=> bool(false) ["ok"]=> float(1) } ["round_trip_time"]=> int(0) } } }
Now my not found database:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
//Ping the database
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
//select the hltv database
$db = $mongo->hltv;
var_dump($db);
returns:
NULL
The error log shows:
PHP Warning: Undefined property: MongoDB\\Driver\\Manager::$hltv in /var/www/hltv/index.php on line 6
When trying to list all Databases:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
//Ping the database
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
//list databases
var_dump($mongo->listDatabases());
returns a blank screen and shows following error:
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\\Driver\\Manager::listDatabases() in /var/www/hltv/index.php:6\nStack trace:\n#0 {main}\n thrown in /var/www/hltv/index.php on line 6
At this point it's pointless to selecta collection.
I made sure that the user root has ReadWrite access and accessing the database with MongoDb Compass is working fine and shows all datbases/collections and lets me read/write data.
My Setup:
OS: Ubuntu 18.04
MongoDB Version: 3.6.3
PHP version: 8.0.3
MongoDB PHP Driver Version: 1.9.1
Apache2 is used to run the PHP code.
Now I found out that there is a Driver and a Library so after installing the Library everything seems to work now. I didn't know that the application was split into 2 parts.

Getting a list of Contacts from Acumatica Web Services API using PHP

I'm trying to use the Acumatica Web Services API to get a list of Contacts (really I'm looking to get ANYTHING, but Contacts are what I'm playing with right now).
I'm successfully able to get a SoapClient connected, but not sure what exactly to do from there to pull a list of all Contacts.
Seeing how you didn't specify Acumatica version or Webservices I'm assuming you are trying to use the "Screen WebAPI" that was in 5.2 and earlier and not the new "Contract Based API" in 5.3
With that in mind, here is an example of how to make the connection and retrieve a list of all of the contacts.
The first step is to utilize the "acuwsdl2php" helper file to generate the needed screen helper classes for PHP.
In the case of contacts:
php acuwsdl2php.php {url of your site}/Soap/CR302000.asmx?WSDL CR302000
This will create the CR302000 subfolder with a Screen.php file that is the php equivalent of the schema for the screen.
Second, here is a sample class that retrieves contact information
<?php
require_once('AcumaticaGate.php');
$client = new AcumaticaGate('{user}', '{password}', 'CR302000','{site}/Soap/');
$Contact_summary = $client->Schema->GetSchemaResult->ContactSummary;
$Contact_detailsummary = $client->Schema->GetSchemaResult->DetailsSummary;
$every_Contact = $Contact_summary->ServiceCommands->EveryContactID;
$Contact = $Contact_summary->ContactID;
$Contact_fname = $Contact_detailsummary->FirstName;
$Contact_lname = $Contact_detailsummary->LastName;
$export_param = new Export();
$export_param->commands = array($every_Contact, $Contact, $Contact_fname, $Contact_lname);
$export_param->filters = array();
$export_param->breakOnError = false;
$export_param->includeHeaders = true;
$export_param->topCount = 0;
$export = $client->Client->Export($export_param);
print_r(var_dump($export));
The output here is something like this:
[177]=>
object(stdClass)#562 (1) {
["string"]=>
array(3) {
[0]=>
string(3) "358"
[1]=>
string(4) "Anna"
[2]=>
string(7) "Johnson"
}
}
[178]=>
object(stdClass)#563 (1) {
["string"]=>
array(3) {
[0]=>
string(3) "359"
[1]=>
string(4) "Yona"
[2]=>
string(5) "Jones"
}
}
The acuwsdl2php and AcumaticaGate files are helper files that Acumatica provided to partners. They might also be available on the client portal for download. A quick google for them though and I believe you can find them on a few public sites.
As a side note, these helper files were originally written for 4.x. You should look at the 5.x guides (assuming you have 5.x) for added information on logging off of a webapi when finished calling it.

Composer Package (PHPLeague) in Codeigniter loading class

I am aiming to use the following package for my project Color Extractor.
I have composer working and setup in my project fine as per #philsturgeon's tutorial here, however I am stuck as the function returns an empty array for an image I know is there.
I am autoloading in the index.php using require FCPATH.'vendor/autoload.php';
And I have tested this using Phil's example.
My Code looks like this:
$client = new League\ColorExtractor\Client();
$image = $client->loadJpeg(FCPATH.'assets/images/tumblr_ma7gmzwfAq1r780z3o1_250.jpg');
// Get the most used color hex code
$palette = $image->extract();
// Get three most used color hex code
$palette = $image->extract(3);
// Change the Minimum Color Ratio (0 - 1)
// Default: 0
$image->setMinColorRatio(1);
$palette = $image->extract();
var_dump($palette);
Can anyone tell me what I am doing wrong here as I don't have any errors in my log and I get the standard output.
array(0) { }
I was being utterly stupid apologies for anyone who viewed this question I was trying to declare multiple variables with the same name and variants of the function.
My final code looks like
$client = new League\ColorExtractor\Client();
$image = $client->loadJpeg(FCPATH.'assets/images/tumblr_ma7gmzwfAq1r780z3o1_250.jpg');
$palette = $image->extract(3);
var_dump($palette);
Which returns an expected array like this.
array(3) { [0]=> string(7) "#E09D73" [1]=> string(7) "#AC4C34" [2]=>
string(7) "#EEDF6C" }

SOAP web service not recognizing ArrayOfAnyType from PHP client for Agemni Database API

I am trying to insert a lead into the SOAP web service with this WSDL: http://www.agemni.com/AgemniWebservices/service1.asmx?WSDL
The API is documented here: http://wiki.agemni.com/4Integration_And_API%27s/APIs/Database_API
This web service's back end code seems to be in .NET.
Here is my code:
// Create soap client.
try {
$this->client = new SoapClient("https://www.agemni.com/AgemniWebservices/service1.asmx?WSDL", array('soap_version' => SOAP_1_2, 'cache_wsdl' => 0, "trace" => true));
} catch (Exception $e) {
$this->logMessage('Couldn\'t connect to web service.');
throw $e;
}
// Create parameters.
$dataParams = new \stdClass;
$dataParams->strUsername = 'userman';
$dataParams->strPassword = 'hissecretpassword';
$dataParams->strCompanyName = 'validcompanyname';
$leadCode = 2;
$dataParams->objecttype = $leadCode;
// Create keys.
$dataParams->keys = new \stdClass;
$dataParams->keys->Phone = 'Phone';
// Create values.
$dataParams->values = new \stdClass;
$dataParams->values->Phone = '8011234567';
// Call web service.
$validateResult = $this->client->CreateEntity($dataParams);
Here is the response I receive:
object(stdClass)#27 (1) {
["ValidateEntityResult"]=>
object(SoapVar)#29 (4) {
["enc_type"]=>
int(0)
["enc_value"]=>
object(stdClass)#28 (10) {
["statusCode"]=>
string(9) "Succeeded"
["status"]=>
string(5) "Error"
["description"]=>
string(56) "Phone number is required and needs to be 10 digits long."
["errorNumber"]=>
int(1)
["xmlResult"]=>
string(39) "<?xml version="1.0" standalone="yes" ?>"
["EntityValidated"]=>
bool(false)
["EntityCreated"]=>
bool(false)
["EntityUpdated"]=>
bool(false)
["EntityIDCreated"]=>
int(0)
["isloggedIn"]=>
bool(false)
}
["enc_stype"]=>
string(15) "ExceptionReport"
["enc_ns"]=>
string(44) "http://tempuri.org/AgemniWebService/Service1"
}
}
I found a similar issue on PHPFreaks that was years old and was never successfully answered. I see other similar issues with nesting parameters in the PHP SoapClient, but all of the suggestions I've seen end in the same error message for me.
The authentication is being received just fine, along with the company name. However, I've stabbed at formatting, parsing, and encoding the keys and values parameters in nearly every way I could conceive. I've played with declaring my parameters as SoapVar and SoapParam with every potential constructor parameter I could think of and still have received the same error message.
I'd greatly appreciate any suggestions as to any reason that someone could suppose that the Agemni web service is not accepting my lead's phone number.
Thanks.
Update
I can just generate the XML request I need in a string and send it using cURL, but I'd like to use SoapClient. However, I have a hard time generating the elements within the keys and values nodes to look like this:
<anyType xsi:type="xsd:string">phone</anyType>'
I followed some guides on creating custom complex types in SoapClient, but I couldn't get it to create XML that looks like that.

what is the correct syntax of BLPOP on Predis?

I do it like this:
$r = new Predis\Client($single_server, $options);
$retval = $r->blpop('queue:query');
But I get this error:
ERR wrong number of arguments for 'blpop' command
Whenever I do this
$r = new Predis\Client($single_server, $options);
$retval = $r->blpop('queue:query',0);
I get the error:
Error while reading line from the server
Doing it from redis-cli
redis 127.0.0.1:6379> BLPOP queue:query
(error) ERR wrong number of arguments for 'blpop' command
redis 127.0.0.1:6379> BLPOP queue:query 0
1) "queue:query"
2) "hello world"
Seems like a bug. The latest version doesn't have this issue, also it apparently dropped namespaces:
<?
include_once "Predis.php";
$r = new Predis_Client();
$retval = $r->blpop('queue:query',0);
var_dump($retval);
?>
It blocked when I accessed the page. Then, I issued LPUSH queue:query 0, went back to the page, and got this:
array(2) { [0]=> string(11) "queue:query" [1]=> string(1) "0" }
Nevertheless, I would recommend the use of phpredis, it is faster than this library, because it is compiled as a php extension. If you have the rights in your server, that's a good pick.

Categories