PHP Fatal error: Class 'FacebookAds\Object\AdAccount' not found in - php

I am using Facebook Ads API to pull data from ads Reporting.
Below is my code :
<?php
use FacebookAds\Object\AdAccount;
$account = new AdAccount('act_xxxx');
$params = array(
'date_preset'=>'last_28_days',
'data_columns'=>"['adgroup_id']",
);
$stats = $account->getReportsStats(null, $params);
foreach($stats as $stat) {
echo "is it inside the foreach loop \n";
echo $stat->impressions;
echo $stat->actions;
}
?>
I get FacebookAds/Object/AdAccount not found. I checked the path and everything looks correct. any idea, what could be the reason for this error. I am not a PHP Expert, so please do correct me, if something is wrong with my code.

<?php
function __autoload($class) {
require_once $class.".php";
}
Save this file as autoload.php in same directory then add below code at start
<?php
require_once('./autoload.php');
Explanation:
In your code you haven't included the file which contains the class FacebookAds\Object\AdAccount. That's why it gives class not found error.
Above code will make sure that all necessary class files are include in code.

Related

Stuck with php function not returning any data

I am expecting the below code to return hi when the project is run.But it's not returning anything .There is no error also.I've started the wamp server. How can i fix this?I'm very new to this. Anyone can tell me this?
functions.php
<?php
function getDate($orderDate, $oderTime) {
return "hi";
}
?>
index.php
<?php
require "vendor/autoload.php";
require "config.php";
require "functions.php";
getDate(var_export($days),var_export($Time));
config.php
<?php
$Time = "1";
$days = ["Sat", "Sun"];
when you use return the return value wil not shown on your screen till you echo it out, like this
echo getDate(var_export($days),var_export($Time));
and by the way you can replace return keyword in your function with echo
in this case you don't need to write echo getDate(var_export($days),var_export($Time));
just type getDate(var_export($days),var_export($Time));

Error while including class from anthor file in php

I want to include a class from file own.php but I am not able to include it as it giving me error as require(class.own.php): failed to open stream: No such file or directory .
I tried all the options i.e include, require, require_once but then also it showing me a error.
include("class/own.php");
own.php
<?php
class own{
public function title(){
$title = $_POST['title'];
echo $title;
}
}
?>
display.php
include("class/own.php");
$obj = new own;
$obj->title();
Your directory structure should be like this
display.php
class/own.php
Then try to include
include("class/own.php");
The use
$obj = new own(); OR $obj = new \own();
$obj->title();
use this:
<?php
class own {
function title() {
$this->model = $_POST['title'];
}
}
// create an object
$test = new own();
// show object properties
echo $test->model;
?>
I think the two files are in the same path (directory), so you are including wrong path. It might be include("own.php");
Considering the following directory structure.
|directory
display.php
own.php
display.php
<?php
include("own.php");
$obj = new own;
$obj->title();
?>
I think you are confusing PHP. To include the file the following code may help
include (__DIR__)."/own.php";

Include and class PHP

Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.

calling a class with ajax

I need some help with this please
I can't get a handle on it.
The problem is that I want to call a class method, in this case with static methods with an ajax call.
I have put the helper class in the same folder as the script that is called by ajax for easy referencing and try to include it.
Could it be that my refencing is wrong?
If I make a testclass in the file that is called by ajax I can get a response.
class test {
public function testit() {
return "testit";
}
}
$t=new test;
$check= $t->testit();
switch($action) {
case "someaction":
$data = array();
$file='input_helper.php';
include_once $file;
$check= input_helper::ip_address();
header('Content-type: application/json');
$output = array(
"check" => $check,
"user" => $data
);
echo json_encode($output);
exit(0); // Stop script.
break;
//...
EDIT FOR MORE CLARIFICATION
The action is set as a post variable in the ajax function
The ajax url points to a script that takes some action based on the posted variables
thanks, Richard
<?php
include_once 'your/class/path/helper_class.php';
.
.
at the top of your PHP page should do it. it really has nothing to do with AJAX. If your PHP file is in fact being hit on the callback, then that should work properly.
Optionally, to test that your path is correct, if you do:
<?php
require 'your/class/path/helper_class.php';
.
.
If the path is not correct PHP will throw a fatal E_ERROR level error.

Howto get filename from which class was included in PHP

I understand that the question is rather hard to understand, I didn't know how to ask it better, so I'll use this code example to make things more clear:
If I have the following files:
test.php:
<?php
include('include.php');
echo myClass::myStaticFunction();
?>
include.php
<?php
__autoload($classname){
include_once("class/".$classname.".php"); //normally checking of included file would happen
}
?>
class/myClass.php
<?php
class myClass{
public static function myStaticFunction(){
//I want this to return test.php, or whatever the filename is of the file that is using this class
return SOMETHING;
}
?>
the magic FILE constant is not the correct one, it returns path/to/myClass.php
in case you need to get "test.php" see $_SERVER['SCRIPT_NAME']
I ended up using:
$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
I am using
$arr = #debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
if (isset($data['file']))
echo $data['file'];
// change it to needed depth
}
This way you don't need to modify the file from which your file is included. debug_backtrace might have some speed consenquencies.

Categories