Error in creating database in MongoDB using composer - php

I'm trying to create a database using composer. I've demo.php as the main file and running the code on localhost.
Error message :
Fatal error: Uncaught
MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable
servers found (serverSelectionTryOnce set): [socket timeout calling
ismaster on '127.0.0.1:27017'] in
C:\xampp\htdocs\phpmongodb\vendor\mongodb\mongodb\src\Database.php:201
Stack trace: #0
C:\xampp\htdocs\phpmongodb\vendor\mongodb\mongodb\src\Database.php(201):
MongoDB\Driver\Manager->selectServer(Object(MongoDB\Driver\ReadPreference))
1 C:\xampp\htdocs\phpmongodb\demo.php(8): MongoDB\Database->createCollection('emplcollection') 2 {main} thrown
in C:\xampp\htdocs\phpmongodb\vendor\mongodb\mongodb\src\Database.php
on line 201
What could be the reason for this?
My code:
<?php
require 'vendor/autoload.php';
$client = new MongoDB\Client;
$companydb = $client->companydb;
$result1 = $companydb->createCollection('emplcollection');
var_dump(result1);
?>

Related

Mongodb Failed to look up SRV record

im new using mongodb,im trying to connect using php based on tutorial, im using ubuntu 20, i read mongodb default doesnt have user password and only accept 127.0.0.1. this script runs in same server as mongodb
<?php
require("vendor/autoload.php");
$client = new MongoDB\Client(
'mongodb+srv://127.0.0.1/test?w=majority'
);
$db = $client->test;
returns :
PHP Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: Failed to parse URI options: Failed to look up SRV record "_mongodb._tcp.127.0.0.1": The specified host is unknown. in /opt/wira/test_mongodb/vendor/mongodb/mongodb/src/Client.php:126
Stack trace:
#0 /opt/wira/test_mongodb/vendor/mongodb/mongodb/src/Client.php(126): MongoDB\Driver\Manager->__construct()
#1 /opt/wira/test_mongodb/test.php(5): MongoDB\Client->__construct()
#2 {main}
thrown in /opt/wira/test_mongodb/vendor/mongodb/mongodb/src/Client.php on line 126

PHP code run in terminal but not working in chrome

I have used pdf-to-image to build my new package. and I got some problem here:
<?php
// die(echo getenv('HOME'););
require_once(__DIR__.'/vendor/autoload.php');
$fileone = realpath('demo.pdf');
$pdf = new Spatie\PdfToImage\Pdf($fileone);
$pdf->saveImage(__DIR__.'upload/');
?>
I required it into index file but I can't run this code on Chrome but it working when I run by terminal, how can I fix it?
UPDATE Adding error shared in comments:
Fatal error: Uncaught ImagickException: Failed to read the file in /opt/lampp/htdocs/pdf2Image/vendor/spatie/pdf-to-image/src/Pdf.php:44 Stack trace: #0 /opt/lampp/htdocs/pdf2Image/vendor/spatie/pdf-to-image/src/Pdf.php(44): Imagick->__construct('/opt/lampp/htdo...') #1 /opt/lampp/htdocs/pdf2Image/index.php(6): Spatie\PdfToImage\Pdf->__construct('/opt/lampp/htdo...') #2 {main} thrown in /opt/lampp/htdocs/pdf2Image/vendor/spatie/pdf-to-image/src/Pdf.php on line 44

How i can connect MongoDB online cloud in PHP

i want online connection with MongoDB cloud in PHP
same source file working properly when i run on localhost in my laptop with empty $server string while i use mongoDb server cloud then i face an error
<?php
session_start();
// $server="";
$server='mongodb+srv://mongo_user_1:<password>#mongodb01-dolny.mongodb.net/test?retryWrites=true&w=majority';
$mongo = new MongoClient($server);
$db=$mongo->blogPosting;
$admin_cl=$db->admin;
$category_cl=$db->category;
$blog_cl=$db->blog;
$contact_cl=$db->contact;
$blog_comment_cl=$db->blog_comment;
?>
Fatal error: Uncaught exception 'MongoConnectionException' with
message '- Found unknown connection string option 'retryWrites' with
value 'true'' in G:\xampp\htdocs\mongo_blog\mongoDb.php:5 Stack trace:
0 G:\xampp\htdocs\mongo_blog\mongoDb.php(5): MongoClient->__construct('mongodb+srv://m...') #1
G:\xampp\htdocs\mongo_blog\blog\index.php(1):
include('G:\xampp\htdocs...') #2 {main} thrown in
G:\xampp\htdocs\mongo_blog\mongoDb.php on line 5

could not find phpSerial class

I have to send a number to my Arduino using the COM port using PHP. I included phpSerial class to do that. Included the PhpSerial.php containing the class phpSerial class to the PHP code. I am getting an error : Fatal error: Uncaught Error: Class 'phpSerial' not found in C:\Apache24\htdocs\kprientvaegan\ref\com.php:4 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\kprientvaegan\ref\com.php on line 4.
Could anyone please help me to fix this?
<?php
include 'PhpSerial.php';
$serial = new phpSerial;
$serial ->deviceSet("COM1"); //Port number
$serial ->confbaudRate(9600);
$serial ->confParity("none");
$serial ->confCharacterLength(8);
$serial ->confStopBits(1);
$serial ->confFlowControl("none");
$serial ->deviceOpen();
$serial ->sendMessage('1');
$serial ->deviceClose();
?>
Fatal error: Uncaught Error: Class 'phpSerial' not found in C:\Apache24\htdocs\kprientvaegan\ref\com.php:4 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\kprientvaegan\ref\com.php on line 4.
I suggest to use pyserial module with python. It is very easy and powerful.
You can to download this module from here:
pyserial module link
And this is the documentation:
pyserial documentation

Fatal error: Uncaught Error in instagram crawler

i want to use https://packagist.org/packages/smochin/instagram-php-crawler
i downloaded that and i made index.php in project
index.php :
<?php
$insta = new Smochin\Instagram\CrawlerTest;
i have this error but i dont know why !
Fatal error: Uncaught Error: Class 'Smochin\Instagram\CrawlerTest' not
found in E:\xamppN\htdocs\insta\index.php:10 Stack trace: #0 {main}
thrown in E:\xamppN\htdocs\insta\index.php on line 10
You just need to add following line to your code
require_once DIR . '/vendor/autoload.php';

Categories