PHP Error, class not found - php

I'm getting the following output on my page when I go to Test.php:
longitude = $long;
$this->latitude = $lat;
}
public function getgeo(){
require_once('lib/mapbox/MapBox.php');
$request = new MapBox('redacted');
$request = $request->reverseGeocode($this->longitude,$this->latitude);
$request = explode(', ',$request[0]['place_name']);
if(count($request)>3){
array_shift($request);
array_splice($request,2,1);
}
$return = array($request[0],$request[2]);
}
}
?>
Fatal error: Uncaught Error: Class 'ReverseGeo' not found in /var/www/html/api.redacted.com/public_html/test.php:8 Stack trace: #0 {main} thrown in /var/www/html/api.redacted.com/public_html/test.php on line 8
Test.php
<?php
require_once(__dir__ . '/classes/reversegeo.php');
$long = '-73.988909';
$lat = '40.733122';
$reversegeo = new ReverseGeo($long, $lat);
$return = $reversegeo->getgeo();
var_dump($return);
?>
classes/reversegeo.php
Class ReverseGeo{
protected $longitude;
protected $latitude;
public function __construct($long, $lat){
$this->longitude = $long;
$this->latitude = $lat;
}
public function getgeo(){
require_once('lib/mapbox/MapBox.php');
$request = new MapBox('redacted');
$request = $request->reverseGeocode($this->longitude,$this->latitude);
$request = explode(', ',$request[0]['place_name']);
if(count($request)>3){
array_shift($request);
array_splice($request,2,1);
}
$return = array($request[0],$request[2]);
}
}
I've confirmed that directories are all correct, file names are correct, etc. and I'm not sure whats going on with this.

Per our discussion in chat, as your php does not support the short opening tag you need to use the full opener. The short tag is the reason that your php source code gets sent directly to the browser and not to the php engine.
You can configure your php settings to allow the short opening tag but its not recommended for portability reasons.
<? should be changed to <?php
As a side note later versions of php no longer need the closing tag at the end of the file so that can be removed if your php supports it.

Related

Converting files using PHP LibreOffice and the ncjoes/office-converter library

I am using ampps as a windows 10 apache server, my php version is 7.3. I downloaded it from the LibreOffice download page and installed it on my computer. Then I installed this library via composer https://github.com/ncjoes/office-converter. I try as in the example given, but it does not convert and gives an error. I would be very grateful if you could help me where I am wrong. Here is my code sample and the error I encountered:
<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';
use NcJoes\OfficeConverter\OfficeConverter;
use PHPUnit\Framework\TestCase;
class OfficeConverterTest extends TestCase
{
/**
* #var OfficeConverter $converter
*/
private $converter;
private $outDir;
public function setUp()
{
parent::setUp();
$DS = DIRECTORY_SEPARATOR;
$file = __DIR__."{$DS}sources{$DS}test.docx";
$this->outDir = __DIR__."{$DS}results";
$this->converter = new OfficeConverter($file, $this->outDir);
}
public function testDocxToPdfConversion()
{
$output = $this->converter->convertTo('result.pdf');
$this->assertFileExists($output);
}
public function testDocxToHtmlConversion()
{
$output = $this->converter->convertTo('result.html');
$this->assertFileExists($output);
}
}
$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
?>
Fatal error: Uncaught Error: Call to a member function convertTo() on null in C:\Program Files\Ampps\www\converter\converter.php:29 Stack trace: #0 C:\Program Files\Ampps\www\converter\converter.php(43): OfficeConverterTest->testDocxToPdfConversion() #1 {main} thrown in C:\Program Files\Ampps\www\converter\converter.php on line 29
When running tests, we are supposed to leave running them to phpunit, but you are trying to call manually (in last 2 lines of your code).
But if you insist on calling manually, change:
$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
Into:
$donustur = new OfficeConverterTest();
$donustur->setUp();
$donustur->testDocxToPdfConversion();
So that you call setUp() which phpunit would normally call for you automatically.
See also:
How do I run all my PHPUnit tests?
How to run single test method with phpunit?
Example for page
If you want to use this logic on a Web-Page, it should look something like:
<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';
use NcJoes\OfficeConverter\OfficeConverter;
echo 'Converting...<br>';
$input = __DIR__ . '/test.docx';
$converter = new OfficeConverter($input, __DIR__.'/results');
$output = $converter->convertTo('result.pdf');
echo 'Saved at:' . $output . '<br>';

This happens only on live server: Fatal error: Uncaught Error: Call to a member function signin() on boolean in

Confession:
I read many similar questions on this platform, but nothing seems closely aligned to my situation. Most of the questions seem to originate from binding params in prepared statements or execute statement.
In my case, the website runs smooth on a local server(Apache2). However, it throws the error below when published on live server.
Fatal error: Uncaught Error: Call to a member function signin() on boolean in /storage/ssd5/815/17670815/app/controllers/signin.php:16 Stack trace: #0 /storage/ssd5/815/17670815/app/core/app.php(33): Signin->index() #1 /storage/ssd5/815/17670815/public_html/index.php(4): App->__construct() #2 {main} thrown in /storage/ssd5/815/17670815/app/controllers/signin.php on line 16
Context
I'm using MVC (OOP) in PHP and here the relevant parts mentioned in the error. I hope this is not too much.
In the main index page, the line referred in the error is a core class(App) instantiation
<?php
session_start();
require_once '../app/initializer.php';
$app = new App(); //this is the line 4 mentioned in the error
In Signin controller class the line referred in the error is indicated below
<?php
class Signin extends Controller{
function index(){
//you can do this if passing data to view
$data["Page_title"] = "Signin";
if($_SERVER['REQUEST_METHOD'] == "POST"){
// this is a debuggin code
//echo "I am signin controller <br />";
// show($_POST);
$user = $this->loadModel("User");
$user->signin($_POST); //this the line referred in the error
}
$this->view("zac/signin",$data);
}
}
In class APP the line is a callback - check below
<?php
class App {
private $controller = "home";
private $method = "index";
private $params = [];
public function __construct()
{
$url = $this->splitURL();
if(file_exists("../app/controllers/".strtolower($url[0]).".php")){
$this->controller = strtolower($url[0]);
//unset the array position
unset($url[0]);
}
require "../app/controllers/".$this->controller.".php";
// echo file_get_contents('http://smart-ecom.000webhostapp.com/app/controllers/'.$this->controller.".php");
//Create instance of whatever controller class is passed(if it exists, otherwise the home controller)
$this->controller = new $this->controller;
if(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method =$url[1];
unset($url[1]);
}
}
$this->params = array_values($url);
call_user_func_array([$this->controller, $this->method],$this->params); //this is line 33
}
/**
* splitURL gets url from browser and processes against the conroller classes and their methods
* #return array
*/
private function splitURL(){
//check if the the GET is set otherwise set the url to defualt class home
$url = isset($_GET['url']) ? $_GET['url'] :"home";
// return explode("/",filter_var(trim($_GET['url'],"/"), FILTER_SANITIZE_URL));
return explode("/",filter_var(trim($url,"/"), FILTER_SANITIZE_URL));
}
}
?>
The Database class's read function is as follows. This method isn't directly referred in the error message
public function read($query, $data = []){
$stmt = self::$conn->prepare($query);
$result = $stmt->execute($data);
if($result){
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(is_array($data) && count($data) > 0){
return $data;
}
}
return false;
}
As I mentioned earlier, this error fires on a live server but the website runs smooth in dev environment with PHP 7.4, Apache2, MySQL 8 Windows 10.
Your help is match appreciated in advance.
I learned this the hard way and I hope this can help someone with similar issues. The cause of the error because of how windows and Linux deals with case sensitivity in file names. In Windows, file names aren't case sensitive while in Linux - they are. So, that was the reason why the website was running smooth in the local dev environment(Windows machine) but throwing an error on a live server(which is Linux). To see the difference, refer to my earlier comment in this thread.
protected function loadModel($model){
if(file_exists("../app/models/". strtolower($model) . ".class.php")){
include "../app/models/".strtolower($model).".class.php";
return $model = new $model();
}else{
return false;
}
}
}
In the "include" line, you can see that I added the strtolower function to include the proper model and that solved the issue.

Only variable should be passed by referenced in Magento problem

In order to see the path to template in generated html source code for debuggin purposes I used the following code snippet in the
app/code/core/Mage/Core/Block/Template.php
/**
* Render block
*
* #return string
*/
public function renderView()
{
$this->setScriptPath(Mage::getBaseDir('design'));
$showDebug = true;
if (!$showDebug) {
$html = $this->fetchView($this->getTemplateFile());
}
else {
$template = $this->getTemplateFile();
$tagName = 'template_'.current(explode('.',end(explode('/',$template))));
$html = '<'.$tagName.'><!-- '.$template.' -->';
$html .= $this->fetchView($template);
$html .= '<!--/ '.$template.' --></'.$tagName.'>';
}
return $html;
}
but now in the error logs I see the following:
2010-12-13T21:55:35+00:00 ERR (3): Strict Notice: Only variables should be passed by reference in /app/code/core/Mage/Core/Block/Template.php on line 245
How should this be referenced in order to avoid this error?
Pretty sure your problem is this line
$tagName = 'template_'.current(explode('.',end(explode('/',$template))));
The end and current methods accept an array variable as a paramater, passed by reference. You're passing the result of a function call, which PHP doesn't like. Assuming that snippet is trying to get an extension-less template name, try this instead
$parts = pathinfo($template);
$tagName = $parts['filename'];
Install the Developer Toolbar extension instead. Or turn on Template Hints from the Admin.

Call to a member function getDOM() on a non-object

I'm trying to create a PHP function that adds an event to a google Calendar. It appears to be building the object correctly but it throws a "Call to a member function getDOM() on a non-object in FeedEntryParent.php" error when trying to add the event. Here is the abbreviated class with only the constructor and the function that adds the event:
class GCal_datasource {
private $user = 'xxxxxxx';
private $pass = 'xxxxxxxxx';
private $client;
private $gdata_cal;
private $calendar;
private $visibility;
public function __construct($settings = NULL){
session_start();
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
if($settings == NULL){
$settings = array();
$settings['calendar'] = 'default';
$settings['visibility'] = 'full';
}
$this->calendar = $settings['calendar'];
$this->visibility = $settings['visibility'];
$this->client = $this->get_ClientLoginHttpClient();
$this->gdata_cal = new Zend_Gdata_Calendar($this->client);
}
public function create_event($fields){
$gc = $this->gdata_cal;
$new_event = $this->gdata_cal->newEventEntry();
echo '<pre>';
print_r($fields);
echo '</pre>';
if(isset($fields['quick_add'])){
$new_event->content = $gc->newContent($fields['quick_add']);
$new_event->quickAdd = $gc->newQuickAdd(true);
} else if (isset($fields['title']) && isset($fields['when'])) {
$new_event->title = $fields['title'];
$where = $gc->newWhere($fields['where']);
$new_event->where = array($where);
$desc = $gc->newContent($fields['desc']);
$new_event->content = $desc;
$new_event->content->type = 'text';
$new_event->when = self::build_when_array($fields['when']);
if(isset($fields['web_content'])){
$new_event->link = web_event_array($fields['web_content']);
}
}
echo '<pre>';
print_r($new_event);
echo '</pre>';
$gc->insertEvent($new_event);
return $created_event->id->text;
}
}
The error (I believe) is how I am calling insertEvent() towards the end of the code. The only reason I think that is I only get the error when it exists and if I remove it the echo above it prints out the Event object as intended.
Anyone with a better grasp of the Goggle PHP API that can lend me a hand I would greatly appreciate.
I had this problem.
I think you have to change string
$new_event->title = $fields['title'];
to
$new_event->title = $service->newTitle($fields['title']);
Call to a member function getDOM() on a non-object in FeedEntryParent.php means that somewhere in the file named "FeedEntryParent.php" you have called getDOM() on a variable and that variable is not an object and so does not have a getDOM() method.
Nowhere in the code you posted is a call to getDOM(), so the error is not generated in the posted code.
Track down that call to getDOM(). The error usually gives you a line number. See what variable you are calling the method on. That variable is not an object. Find where that variable is set - that's probably where your problem is.

PHP5 and Microsoft Live Search 2.0

I'm trying to use Live Search 2.0 but even a simple example doesn't seem to work. Microsoft only has example code for 1.1 and they're not giving out AppIDs for that version.
Here's what I'm trying with:
<?php
$server = new SoapClient('http://soap.search.msn.com/webservices.asmx?wsdl');
class Search {
public $Request;
}
class SearchRequest {
public $AppID;
public $Query;
public $CultureInfo;
public $SafeSearch;
public $Flags;
public $Location;
public $Requests;
}
class SourceRequest {
public $Source;
public $Offset;
public $Count;
public $FileType;
public $SortBy;
public $ResultFields;
public $SearchTagFilters;
}
$searchRequest = new SourceRequest();
$searchRequest->Source = 'Web';
$searchRequest->Offset = 0;
$searchRequest->Count = 5;
$searchRequest->ResultFields = 'All SearchTagsArray';
$request = new SearchRequest();
$request->AppID = '...';
$request->Query = 'Bill Gates';
$request->CultureInfo = 'en-US';
$request->SafeSearch = 'Off';
$request->Flags = '';
$request->Requests = array($searchRequest);
$search = new Search();
$search->Request = $request;
$server->Search($search);
?>
AppID is correctly specified in the code: I just erased it from here. I'm getting the following error:
Array ( [0] => SearchResponse Search(Search $parameters) )
Fatal error: Uncaught SoapFault exception: [soapenv:Client] Client Error in /Users/thardas/Sites/vt9/widgets/ms_livesearch.php:41
Stack trace:
#0 [internal function]: SoapClient->__call('Search', Array)
#1 /Users/thardas/Sites/vt9/widgets/ms_livesearch.php(41): SoapClient->Search(Object(SearchRequest))
#2 /Users/thardas/Sites/vt9/index.php(23): include('/Users/thardas/...')
#3 {main} thrown in /Users/thardas/Sites/vt9/widgets/ms_livesearch.php on line 41
You could begin by using the proper soap api url for 2.0.
It's now
"http://api.search.live.net/search.wsdl?AppID=YourAppId" taken from (http://msdn.microsoft.com/en-us/library/dd250965.aspx )
You can also use the new JSON api with php.
$appid = 'Your app id';
$searchitem = 'PHP Manual';
$request = 'http://api.search.live.net/json.aspx?Appid=' . $appid . '&sources=web&query=' . urlencode( $searchitem);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
foreach($jsonobj->SearchResponse->Web->Results as $value)
{
//$value->Url
//$value->Title
//$value->Description
}
And finally theres a xml api you can look up at the msdn link as well and it can be fetched essentially the same way as the json you just need to decode it differently.
The sample code for API 2.0 is on MSDN but we do not have the complete PHP code sample out yet.
A first code sample (very similar to the one in the answer you already got) in included in the blog post on the Live Search Developer Blog
You may be aware that there are currently some issues with SOAP in PHP 5.2.6 - the Live Search service seems to be affected by it in both 1.1 and 2.0. The simplest workaround is to use another interface (JSON or XML)

Categories