I'm trying to implement Google APIs in PHP on WAMP, but I can't even manage to include the files.
My code is the following :
<?php
require_once "./vendor/google/apiclient/src/Google/Client.php";
Client.php
require_once 'Google/Auth/AssertionCredentials.php';
require_once 'Google/Cache/File.php';
require_once 'Google/Cache/Memcache.php';
....
My error :
Warning: require_once(Google/Auth/AssertionCredentials.php): failed to
open stream: No such file or directory in
C:\wamp\www\mysite\vendor\google\apiclient\src\Google\Client.php on
line 18
How should I use include path, and similar commands to get Client.php to find what it wants ?
There are multiple ways so that the file can be found. These include the absolute path or also using setting the include path. The include path can be set via:
php.ini
.htaccess
ini_set function
set_include_path function
Please goole the one most appropriate for your use
I solved my problem using Composer.
Related
I tried to use the absolute path to include my files :
I have 4 files (I have other file on my localhost but oddly the inclusion works well) :
header.php (C:\wamp\www\MySite\layout\header.php)
<?php session_start ();
require_once '/pdo.php';
....
pdo.php (C:\wamp\www\MySite\pdo.php)
<?php
require_once '/class/User.php';
require_once '/class/Order.php';
....
forms/login.php (C:\wamp\www\MySite\forms\login.php)
<?php
session_start ();
include '/pdo.php';
....
login.php (C:\wamp\www\MySite\login.php)
<?php
$title = 'Connexion';
include ("/layout/header.php");
...
So it's look like :
Root
- forms
- login.php
-layout
- header.php
- pdo.php
- login.php
And I have this errors :
( ! ) Warning: include(/pdo.php): failed to open stream: No such file or directory in C:\wamp\www\MySite\forms\login.php on line 3
Call Stack
( ! ) Warning: include(): Failed opening '/pdo.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\MySite\forms\login.php on line 3
( ! ) Fatal error: Class 'User' not found in C:\wamp\www\MySite\forms\login.php on line 12
But I have this problem on a lot of files since I wanted to change the arboresence (tree) of files and folder ..
How I can solve this problem ? and how I can do to avoid this problem in the future ?
Thank you
I tried to use the absolute path to include my files :
header.php (C:\wamp\www\MySite\layout\header.php)
<?php session_start ();
require_once '/pdo.php';
The PHP code is executed on the server. An absolute path in this context means a file-system absolute path, not a web host path. Since you are on Windows, /pdo.php in fact means C:/pdo.php and not C:\wamp\www\MySite\pdo.php as it seems you think.
The best way to work with paths in PHP, regarding include and require is to use the __FILE__ and __DIR__ magic constants and the dirname() PHP function to build the (file-system) absolute paths of files starting from their relative locations.
Your code becomes:
header.php (C:\wamp\www\MySite\layout\header.php):
<?php
session_start ();
// 'pdo.php' is one level up
require_once dirname(__DIR__).'/pdo.php';
....
pdo.php (C:\wamp\www\MySite\pdo.php)
<?php
// User.php is inside the 'class' subdirectory
require_once __DIR__.'/class/User.php';
require_once __DIR__.'/class/Order.php';
....
dir1/dir2/dir3/file.php (C:\wamp\www\MySite\dir1\dir2\dir3\file.php)
<?php
// 'header.php' is in the 'layout' subdirectory of the grand-grand parent directory
include dirname(dirname(dirname(__DIR__))).'/layout/header.php';
Remark
The solution presented here makes the code independent of its actual location in the file system. You can move the entire project (everything in C:\wamp\www\MySite) in a different directory or on a different computer and it will work without changes. Even more, if you use forward slashes (/) as directory names separators it works on Windows, macOS or any Linux flavor.
One convention is to include a configuration file in every php script. This configuration file would set the include path, allowing you to include other files, classes, etc and would continue to work regardless of whether your current working directory had changed - it will allow you to better organise your classes and functions into meaningful directories and include them without worrying about the full path:
An example below:
Create file at C:\wamp\www\MySite\config.php
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'C:\wamp\www\MySite\class' . PATH_SEPARATOR . 'C:\wamp\www\MySite\conf');
?>
Then in
header.php (C:\wamp\www\MySite\layout\header.php)
<?php
require_once('C:\wamp\www\MySite\config.php');
session_start ();
require_once '/pdo.php'; // put pdo.php in C:\wamp\www\MySite\conf\ directory and it will be included
....
In pdo.php (C:\wamp\www\MySite\pdo.php):
<?php
require_once 'User.php';
require_once 'Order.php';
....
Since moving my website from local to online , I have a path problem using require.
I have pages that call bootstrap.php like this:
require 'inc/bootstrap.php';
And boostrap.php looks like this:
<?php
spl_autoload_register('app_autoload');
function app_autoload($class){
require "class/$class.php";
}
This worked well in local.
Now, online, I get the following message:
Warning: require_once(/home/website/www/class/functions.php): failed to open stream: No such file or directory in /home/website/www/inc/bootstrap.php on line 6
Fatal error: require_once(): Failed opening required '/home/website/www/class/functions.php' (include_path='.:/usr/share/php:/usr/share/pear')
in /home/website/www/class/functions.php on line 6
So I thought, I should put an absolute path in boostrap.php like this:
<?php
spl_autoload_register('app_autoload');
function app_autoload($class){
$path = $_SERVER['DOCUMENT_ROOT'] . "class/$class.php";
require_once "$path";
}
But I get the exact same error.
I dont understand why it is still looking in the /home/website/www/inc/bootstrap.php and not following the absolute path which is /home/website/www/class/functions.php ?
EDIT:
After testing the different solutions using absolute pathes, I am still getting the error that "No such file or directory in /home/website/www/bootstrap.php: so it is still looking in the file instead of directory.
Could it be because I am using a double require? I first require boostrap.php from description.php (which works fine) and then I require class.php from this boostrap.php (which doesent take the absolute path but the path corresponding to the file boostrap.php ?
ANSWER:
Ok it finally works with this configuration:
First file using require:
require_once(dirname(__FILE__). '/inc/bootstrap.php');
and boostrap.php:
require_once(dirname(__FILE__). "/../class/$class.php");
As it was said in comments, it is safe to use absolute path instead of 'inc/...', cause it is relative to current class execution path, i.e.:
define ('BASE_PATH', dirname(__FILE__).'/../');
Then use BASE_PATH.{your_path} to access files.
I suggest to use require_once to avoid multiple inclusions.
Somehow, I cannot include the file I want, neither using require_once, nor include. The file I want to include is in the same directory as the file in which I want to include it. I tried every combination, but still does not work.
The error I get trying to use require_once
Warning: require_once(D:\PROJEKAT\wamp\www\Eif\db_connect.php): failed to open stream: No such file or directory in D:\PROJEKAT\wamp\www\Eif\create_user.php
I first tried with
require_once '/db_connect.php'
then I used
require_once '\db_connect.php' cause I realized that I have to use \ in windows
Then tried
require_once __DIR__ . '\db_connect.php'
And lastly
require_once $_SERVER['DOCUMENT_ROOT'] . '/Eif/db_connect.php';
I think that the path I'm using is good, but it keeps using it on the wrong way.
What is the problem?
Thanks
Quick answer
You should be able to do this:
require_once 'db_connect.php';
Furthermore information
require, require_once, include and include_once are all the same with one exception: require will emit a fatal error on failure where as include will emit warning.
The other thing to bare in mind these actually search through the php ini include_path directive, so when you attempt to include another file php will search through those directories in order of the first directive.
By default this directive usually looks like: .:/usr/share/php:/usr/share/pear. This could be changed by a number of functions, even the php.ini which you would want to be aware of:
set_include_path - Set the include paths directive;
get_include_path - Show the current include paths;
getcwd - Show the current working directory;
chdir - Change the current working directory;
NOTE if using set_include_path you are not adding to the list, but overriding it's value; so it would be wise to know the correct way of setting this is:
set_include_path(get_include_path() . PATH_SEPERATOR . '/path/to/new/location');
If the file you want to include are in the same directory this should work:
require_once 'db_connect.php'
There has to be some mistake in the way you are trying to reach to the path.
You can use following php functions to debug it.
<?php
print_r(get_included_files()); // Write this above the file that you are trying to include
set_include_path('path_to_your_directory');
// This will set the directory for once in<br /> your page after setting the path use require_once with the file name straight away
?>
i am using the PHP function require_once to require a file:
<?php require_once 'reviewtickets_history.php?seq='.$_GET["seq"].'&type=history' ;?>
but i am getting the error:
Warning: require_once(reviewtickets_history.php?seq=34844&type=history) [function.require-once]: failed to open stream: No such file or directory in /home/user/public_html/admin/helpdesk/reviewtickets.php on line 316
but if i remove my .php file from the URL and enter in the .php page in the brackets above (reviewtickets_history.php?seq=34844&type=history) it displays fine
Option 1: To require a file, use local path.
require_once '/path/to/reviewtickets_history.php';
This will include the full script source, and evaluate the PHP code in your current file. You cannot pass query parameters with this method.
Option 2: To require a URL, use a full URL.
require_once 'http://mydomain.com/reviewtickets_history.php?myparam=val';
This will include the output of the script, not the PHP source. You can use query parameters here.
Note that allow_url_fopen has to be enabled for this second option to work.
in require_once need to set a path on the server to the file but not a URL. For example you can't pass http://www.example.com/somefile.php but you can do /var/www/folder/somefile.php.
In case then you
require_once 'http://mydomain.com/reviewtickets_history.php'
it will include the output and not the php functions
I'm a PHP newb trying out the YouTube API demo at http://www.youtube.com/watch?v=LMhN6pCAZWo.
I have the google-api-php-client and yt-samples-php-master directories that he says to download at
www.mysite.com/video/google-api-php-client and
www.mysite.com/video/yt-samples-php-master
the search.php file is in yt-samples-php-master and I have replicated the video and set:
if ($_GET['q'] && $_GET['maxResults']) {
set_include_path("./google-api-php-client/src");
// Call set_include_path() as needed to point to your client library.
require_once 'Google_Client.php';
require_once 'contrib/Google_YouTubeService.php';
I can get to www.mysite.com/videos/yt-samples-php-master/search.php fine but when I search I get message:
Warning: require_once(Google_Client.php) [function.require-once]: failed to open stream: No such file or directory in /home/myusername/mysite.com/videos/yt-samples-php-master/search.php on line 17
Fatal error: require_once() [function.require]: Failed opening required 'Google_Client.php' (include_path='./google-api-php-client/src/') in /home/myusername/mysite.com/videos/yt-samples-php-master/search.php on line 17.
I noticed that the files Google make available have a typo require_once 'contrib/Google_YoutubeService.php';
should be require_once 'contrib/Google_YouTubeService.php';
but that doesn't seem to help. Any clues would be very gratefully received!
EDIT: I've also set 755 recursively through videos directory to sub-directories and files
It's late. but someone may be looking for this in future.
Solution:
Google's php client uses composer, so, you've to put the path to the autoload file.
Link: Google API PHP client
Remove or comment out require_once() methods for Google_Client.php & contrib/Google_YoutubeService.php
Rename class name from Google_YoutubeService to Google_Service_YouTube while instantiating
Just replace this ./ and try this following format
set_include_path("google-api-php-client/src");
Is google-api-php-client in the same folder as your project. Make sure you are setting the include to point to that directory. And that directory is 755'ed.
Also in latest (v1.0) php library they changed placing of sources to
require_once 'Google/Client.php';
require_once 'Google/Service/Youtube.php';
OK I've come back to this after a little while. I've managed to get the search.php example to work by:
In search.php changing the require statements to:
require_once './google-api-php-client/src/Google_Client.php';
require_once './google-api-php-client/src/contrib/Google_YouTubeService.php';
and commenting out the set_include_path and following two require_once lines.
This lead to a fatal error saying that class Google_Service_YouTube could not be found in Google_YouTubeService.php. In that file there was however a class called Google_YouTube_Service which I renamed and then it worked!