MongoDB Connection using PHP - Fatal Error - php

I am trying to connect mongodb using php script, below are the details:
PHP Version 7.2.12
Mongo Version: v3.2.18
Code:
$hostPort = "76.34.28.76:27017/mydb";
$username = "test";
$db = "mydb";
$p = "test%402018";
$manager = new MongoDB\Driver\Manager("mongodb://$username:$p#$hostPort", [
"connectTimeoutMS" => 2000,
"socketTimeoutMS" => 3000,
]);
var_dump($manager);
$filter = array();
$options = [
'ssl'=>false
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('db.tmp_collection', $query);
foreach ($cursor as $document) {
var_dump($document);
}
While executing the above script I am getting below error:
Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set): [connection timeout calling ismaster on '76.34.28.76:27017'] in C:\xampp\htdocs\index.php:19 Stack trace: #0 C:\xampp\htdocs\index.php(19): MongoDB\Driver\Manager->executeQuery('db.tmp_collection', Object(MongoDB\Driver\Query)) #1 {main} thrown in C:\xampp\htdocs\index.php on line 19

Related

soap not connecting in server PHP

Can anyone plz help
I have written a soap server and a client to test and it works in localhost and also in the server but when both are in save place but when I try to connect the localhost to the server it would not work. gives error
Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in D:\xampp\htdocs\PN Encryptor\service\client.php:9 Stack trace: #0 D:\xampp\htdocs\PN Encryptor\service\client.php(9): SoapClient->__soapCall('hello', Array) #1 {main} thrown in D:\xampp\htdocs\PN Encryptor\service\client.php on line 9
my code
///server
class EncSoapServer
{
public function hello($id){
return "Hello WOrld";
}
}
$options = ['uri' => 'http://prageethniranjan.epizy.com/service'];
$server = new SoapServer(null, $options);
$server->setClass('EncSoapServer');
$server->handle();
/// Client
$client = new SoapClient(null, array(
'location' => "http://prageethniranjan.epizy.com/service/encript.php",
'uri' => "http://prageethniranjan.epizy.com/service",
'trace' => 1 ));
//echo $return = $client->__soapCall("encrypt",array("4.1","this is a text message","Niranjan",1979,8,true));
try{
$responce = $client->__soapCall("hello",array('id'=>1));
echo $responce;
}
catch(SoapFault $e)
{
$response = $client->__getLastResponse();
echo $response;
$response = str_replace("&#x1A",'',$response); ///My Invalid Symbol
$response = str_ireplace(array('SOAP-ENV:','SOAP:'),'',$response);
$response = simplexml_load_string($response);
echo $response;
}
i try running on localhost it works
and put both script on server it also works
but could not connect it with server

Returning Successful ping but after a fatal error

I am using the Solr library.And I have this code:
<?php
namespace Solarium;
use Solarium;
require 'vendor/autoload.php';
require(__DIR__.'\solarium\init.php');
htmlHeader();
// check solarium version available
echo 'Solarium library version: ' . Client::VERSION . ' - ';
// create a client instance
$config = array(
'endpoint' => array(
'localhost' => array(
'host' => 'localhost',
'port' => 3306,
'path' => '/solarium/',
)
)
);
$client = new Solarium\Client($config);
// create a ping query
$ping = $client->createPing();
// execute the ping query
try {
$result = $client->ping($ping);
echo 'Ping query successful';
echo '<br/><pre>';
var_dump($result->getData());
echo '</pre>';
} catch (Solarium\Exception $e) {
echo 'Ping query failed';
}
htmlFooter();
Well.It sorta works.
It gives me this:
Solarium library version: 3.0.0 - Ping query successful
Fatal error: Uncaught Solarium\Exception\UnexpectedValueException: Solr JSON response could not be decoded in C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\solarium\solarium\library\Solarium\Core\Query\Result\Result.php:158
Stack trace:
#0 C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\ping4.php(34): Solarium\Core\Query\Result\Result->getData()
#1 {main}
thrown in C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\solarium\solarium\library\Solarium\Core\Query\Result\Result.php on line 158
So The ping was successful.but I get a fatal error after a successful ping.
What am I supposed to get? And what am i doing wrong?
Please help.

MongoDB Remote Connections

I installed MongoDB the last version
and i want to connect to MongoDB with an other server with this test code.
- installed MongoDB Driver in PHP Server
- Allowed PHP Server IP in MongoDB iptabels:
PHP Code:
<?php
$m = new MongoClient("mongodb://X.X.X.X:27017");
$dbname = $m->selectDB("Test");
$collection = $dbname->selectCollection("Shows");
$data1 = array(
'user_id' => 'salam',
'age' => 20,
'Status' => 'Allll'
);
$dbname = $collection->insert($data1, array('$data1' => 1));
$results = $collection->find();
echo "<pre>";
foreach($results as $test) {
print_r($test);
}
echo "</pre>";
?>
ERROR:
atal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: X.X.X.X.X.X:27017: Connection timed out' in /home/payam48/domains/.../public_html/includes/classes/class.mongodatabase.php:9 Stack trace: #0 /home/payam48/domains/.../public_html/includes/classes/class.mongodatabase.php(9): MongoClient->__construct('mongodb://178.1...') #1 /home/payam48/domains/.../public_html/includes/classes/class.doctor.php(519): Mongodatabase->__construct('1c5b98782e44ccd...', 'b2f202952b685b6...') #2 /home/payam48/domains/.../public_html/includes/classes/class.doctor.php(486): Doctor->online_delete() #3 /home/payam48/domains/.../public_html/application/controllers/managedoctor.php(27): Doctor->delete('4') #4 /home/payam48/domains/.../public_html/application/core/router.class.php(118): managedoctorController->delete() #5 /home/payam48/domains/.../public_html/index.php(9): router-> in /home/payam48/domains/.../public_html/includes/classes/class.mongodatabase.php on line 9

Get Sharepoint data list in php

I tried to get SharePoint list in php. Below is code
require_once __DIR__ . '\Faker-master\src\autoload.php'; //Faker library (https://github.com/fzaninotto/Faker)
require_once 'lib/SPOClient.php';
$username = 'aaaa.aaa#microsoft.com';
$password = '*******';
$url = "https://microsoft.sharepoint.com";
generateContacts($url,$username,$password);
function generateContacts($url,$username,$password){
$client = new SPOClient($url);
$client->signIn($username,$password);
$list = $client->getList('ClientMaster');
$contactsCount = 120;
for($i = 0; $i < $contactsCount; $i++){
$contactEntry = createContactEntry();
$item = $list->addItem($contactEntry);
print "Contact '{$item->Title}' has been created succesfully.\r\n";
}
}
O/p:
Fatal error: Uncaught exception 'Exception' with message 'Invalid STS request. ' in E:\Apps\xampp\htdocs\ep\lib\SPOClient.php:260 Stack trace: #0 E:\Apps\xampp\htdocs\ep\lib\SPOClient.php(242): SPOClient->processToken('requestToken('ssss.kkkk...', '*****') #2 E:\Apps\xampp\htdocs\ep\listexamples.php(24): SPOClient->signIn('ssss.kkkk...', '******') #3 E:\Apps\xampp\htdocs\ep\listexamples.php(15): generateContacts('https://worldem...', 'sssss.kkk...', '******') #4 {main} thrown in E:\Apps\xampp\htdocs\ep\lib\SPOClient.php on line 260

Mongodb-PHP app connection

For connecting Mongodb with PHP application, i installed Mongodb Driver in windows and extension was enabled (checked in phpinfo()). Then i execute the folloeing php code
<?php
// Config
$dbhost = 'localhost';
$dbname = 'test';
// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
// select the collection
$collection = $db->shows;
// pull a cursor query
$cursor = $collection->find();
foreach($cursor as $document) {
var_dump($document);
}
?>
and it returns a fatal error. How can solve this?
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: No connection could be made because the target machine actively refused it. ' in C:\xampp\htdocs\check\index.php:7 Stack trace: #0 C:\xampp\htdocs\check\index.php(7): MongoClient->__construct('mongodb://local...') #1 {main} thrown in C:\xampp\htdocs\check\index.php on line 7
This is connection and test wode. check whether it is working in your system or not :
<?php
$m = new MongoClient("localhost");
$dbname = $m->selectDB("Test");
$collection = $dbname->selectCollection("Shows");
$data1 = array(
'user_id' => 'abc',
'age' => 20,
'Status' => 'A'
);
$dbname = $collection->insert($data1, array('$data1' => 1));
$results = $collection->find();
echo "<pre>";
foreach($results as $test) {
print_r($test);
}
echo "</pre>";
?>
Have you started mongod server , i could replicate your error only when i closed mongod server .

Categories