instagram php scraping (gives error) - php

I am pretty new to PHP so this may be a dumb question but yeah. I'm trying to get information about a user on instagram with this library: https://github.com/postaddictme/instagram-php-scraper
I've added the library and also the Unirest library so that it should work?
I also have this code in my index.php file:
<?php
require_once dirname(__FILE__) . '/Unirest/Exception.php';
require_once dirname(__FILE__) . '/Unirest/Method.php';
require_once dirname(__FILE__) . '/Unirest/Response.php';
require_once dirname(__FILE__) . '/Unirest/Request.php';
require_once dirname(__FILE__) . '/Unirest/Request/Body.php';
require_once dirname(__FILE__) . '/InstagramScraper/Instagram.php';
require_once dirname(__FILE__) . '/InstagramScraper/Endpoints.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Account.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Comment.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Location.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Media.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Tag.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramException.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramAuthException.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramNotFoundException.php';
use InstagramScraper\Instagram;
$account = Instagram::getAccountById(272308256);
echo $account->username;
?>
But this code does not want to run :/. I keep getting this error:
Fatal error: Using $this when not in object context in
C:\xampp\htdocs\Instagramposts\InstagramScraper\Instagram.php on line 313
I mean if it's a finished library it should immediately work? Anybody's got a solution?

You need to create an instance of the class (object) first before using the method. The method makes use of the "this" operator which needs an object.
Your code should look like this:
use InstagramScraper\Instagram;
$insta = new Instagram;
$account = $insta->getAccountById(272308256);
echo $account->username;

Related

Unable to trace require_once error in PHP

i have a file structure like this in my PHP 7.1
Model/Abstract.php
Model/NTLStm.php
Model/SoapCl.php
Controller.php
i Called Model/Abstract.php in Controller like this:
use Model/Abstract as modelAbstract;
$abstract = new modelAbstract();
i tried to include the Model/NTLStm.php & Model/SoapCl.php in Model/Abstract.php like this:
defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
require_once dirname(__FILE__) . DS . 'SoapCl.php';
require_once dirname(__FILE__) . DS . 'NTLStm.php';
echo 'success';
but it seems it always terminate the process in require_once , i already tried to put try catch like this:
try{
defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
require_once dirname(__FILE__) . DS . 'SoapCl.php';
require_once dirname(__FILE__) . DS . 'NTLStm.php';
} catch(\Exception $e){
echo $e->getMessage();
}
but it won't print anything
Try using parentheses with require_once... like this:
require_once (dirname(__FILE__) . DS . 'SoapCl.php');
require_once (dirname(__FILE__) . DS . 'NTLStm.php');

Class Not found when autoloading with namespace (no Composer)

I have been checking for answers but with no success. I'm not using composer autoload function.
I'm trying to test some classes, Collections and traits in a index.php.
The problem is when i'm including a namespace, I'have created the _autoload function (No composer) to load a class when needed. If I execute php index.php i'm getting the class not found error.
I have tried the use command, but with no success as well.
It solves when I include each class one by one like this:
include_once __DIR__ . '/Collection.php';
include_once __DIR__ . '/User.php';
include_once __DIR__ . '/Tweet.php';
include_once __DIR__ . '/UserCollection.php';
include_once __DIR__ . '/TweetCollection.php';
Thats my index.php:
<?php
namespace phpexercises\entregableT3;
error_reporting(E_ALL);
ini_set( "display_errors", "on" );
/*
include_once __DIR__ . '/Collection.php';
include_once __DIR__ . '/User.php';
include_once __DIR__ . '/Tweet.php';
include_once __DIR__ . '/UserCollection.php';
include_once __DIR__ . '/TweetCollection.php';
*/
use phpexercises\entregableT3\User;
function __autoload($classname){
require __DIR__ . "/" . $classname . ".php";
}
//UserCollection pruebas
$alex = new User();
$jumbo = new User();
$users = new UserCollection();
$alex->phone = "682383";
$alex->email = "alex#example.com";
$alex->city = "BCN";
$alex->gender = "Male";
$jumbo->phone = "54534535";
$jumbo->email = "jumbo#example.com";
$jumbo->city = "elche";
$jumbo->gender = "Male";
$users->add($alex);
$users->add($jumbo);
print_r($users->findByEmail("alex#example.com"));
print_r($users->findByGender("Female"));
//Tweet Collection pruebas
$tweet = new Tweet();
$tweet->email = "alex#example.com";
$tweet->text = "This is a tweet";
$tweet->date = date("D:M:Y");
$tweet1 = new Tweet();
$tweet1->email = "alex#example.com";
$tweet1->text = "This is a tweet";
$tweet1->date = date("D:M:Y");
When I uncomment the requires, everything works fine, but it is not the way to load all classes...Any idea for this behaviour? How should I load the classes at same time?
Thanks

After wordpress upgrade I'm getting wp_user not found in pluggable.php

I am calling a php script from an app that checks the credentials of a wp user. This worked fine until I upgraded wordpress from 4.3 to 4.4 (and 4.5). It's really time I got this sorted but I can't think why wp-user is no longer available as it is in the include list.
error: wp_user not found in pluggable.php
Please see the code below..
define( 'SHORTINIT', TRUE );
require_once $abspath . '/wp-load.php';
require_once $abspath . '/wp-includes/user.php';
require_once $abspath . '/wp-includes/pluggable.php';
require_once $abspath . '/wp-includes/formatting.php';
require_once $abspath . '/wp-includes/capabilities.php';
require_once $abspath . '/wp-includes/kses.php';
require_once $abspath . '/wp-includes/meta.php';
require_once $abspath . '/wp-includes/l10n.php';
require_once $abspath . '/wp-includes/class-wp-error.php';
require_once $abspath . '/wp-includes/general-template.php';
require_once $abspath . '/wp-includes/link-template.php';
$the_authenticate = wp_authenticate_username_password('null',$user_name,$user_password);
if( is_wp_error( $the_authenticate ) ) {
echo '{"error":"The username was not recognised"}';
}
else
{
$the_user_authenticate_id = $the_authenticate->ID;
$the_user = get_user_by('login', $user_name);
$the_user_id = $the_user->ID;
if ( !$the_user )
{
//echo "{'error':'The username was not recognised'}";
}
I have found the solution is that I now need to include the following..
require_once $abspath . '/wp-includes/class-wp-roles.php';
require_once $abspath . '/wp-includes/class-wp-user.php';
require_once $abspath . '/wp-includes/class-wp-role.php';
Please can anyone explain why these extra includes are suddenly necessary in wordpress 4.4 and whether my solution makes sense?
Download the updated wordpress version from wordpress.org and replace your current wp-admin and wp-includes folders with updated wordpress folders.
Hope it will work.

How do I include a path of google client library in php

$r = set_include_path(get_include_path() . '\google-api-php-client-master\src');
echo $r;
require_once 'Google/Client.php';
require_once 'Google/Service/AdExchangeSeller.php';
function __autoload($class_name) {
include 'examples/' . $class_name . '.php';
}
when I am running it on browser its showing following error..
You are not using the path seperator when calling set_include_path, try this:
$r = set_include_path(get_include_path() . PATH_SEPARATOR . '\google-api-php-client-master\src');
Here is the documentation on set_include_path
Edit
Did you restart Apache after making the change to your php.ini?

How to use Piwik device detector in php project?

I want to use php device detector that is part of famous Piwik project, but i can't understand how to include and use the code in my php code? i don't want to use composer.
I wrote:
<?php
include 'DeviceDetector.php';
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
$dd = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);
$dd->parse();
$clientInfo = $dd->getClient();
var_dump($clientInfo);
But it doesn't work. i get this error:
Fatal error: Uncaught exception 'Exception' with message 'client parser not found' in D:\DeviceDetector.php:214
Stack trace:
#0 D:\DeviceDetector.php(136): DeviceDetector\DeviceDetector->addClientParser('FeedReader')
#1 D:\index.php(67): DeviceDetector\DeviceDetector->__construct('Mozilla/5.0 (Wi...')
#2 {main}
thrown in D:\DeviceDetector.php on line 214
// I figured it out. Pretty easy. Grab a copy of master and make a few mods.
// At the top of DeviceDetector.php and in this order:
namespace DeviceDetector;
require_once (dirname(__FILE__).'/spyc.php');
require_once (dirname(__FILE__).'/Cache/Cache.php');
require_once (dirname(__FILE__).'/Cache/StaticCache.php');
require_once (dirname(__FILE__).'/Parser/ParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Bot.php');
require_once (dirname(__FILE__).'/Parser/OperatingSystem.php');
require_once (dirname(__FILE__).'/Parser/VendorFragment.php');
require_once (dirname(__FILE__).'/Parser/Client/ClientParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Device/DeviceParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Client/Browser/Engine.php');
// Add as the first line of addClientParser():
require_once (dirname(__FILE__).'/Parser/Client/'.$parser.'.php');
// Add as the first line of addDeviceParser():
require_once (dirname(__FILE__).'/Parser/Device/'.$parser.'.php');
// You'll also have to grab a copy of spyc.php - google it - easy to find.
// That's it. Works awesome. Faster than anything else.
To me I worked well. For DeviceDetector version 3.7.3:
namespace DeviceDetector;
require_once(dirname(__FILE__) . '/Cache/Cache.php');
require_once(dirname(__FILE__) . '/Cache/StaticCache.php');
require_once(dirname(__FILE__) . '/Parser/ParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Bot.php');
require_once(dirname(__FILE__) . '/Parser/OperatingSystem.php');
require_once(dirname(__FILE__) . '/Parser/VendorFragment.php');
require_once(dirname(__FILE__) . '/Parser/Client/ClientParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Device/DeviceParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine/Version.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Yaml/Parser.php');
require_once(dirname(__FILE__) . '/Yaml/Spyc.php');
//Same as before, you'll need to find your own copy of spyc.php. Here is how I add it (pulls from a directory above the library):
require_once(realpath(dirname(__FILE__) . '/..') . '/spyc.php');
//Add as the first line of addClientParser():
require_once(dirname(__FILE__) . '/Parser/Client/FeedReader.php');
require_once(dirname(__FILE__) . '/Parser/Client/MobileApp.php');
require_once(dirname(__FILE__) . '/Parser/Client/MediaPlayer.php');
require_once(dirname(__FILE__) . '/Parser/Client/PIM.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Parser/Client/Library.php');
//Add as the first line of addDeviceParser():
require_once(dirname(__FILE__) . '/Parser/Device/HbbTv.php');
require_once(dirname(__FILE__) . '/Parser/Device/Console.php');
require_once(dirname(__FILE__) . '/Parser/Device/CarBrowser.php');
require_once(dirname(__FILE__) . '/Parser/Device/Camera.php');
require_once(dirname(__FILE__) . '/Parser/Device/PortableMediaPlayer.php');
require_once(dirname(__FILE__) . '/Parser/Device/Mobile.php');
For those not using an autoloader, here is a solution that works with Device Detector version 3.10.1 based on Erick's answer for previous versions:
//Same as before, add this to the top of DeviceDetector.php in this order:
namespace DeviceDetector;
require_once(dirname(__FILE__) . '/Cache/Cache.php');
require_once(dirname(__FILE__) . '/Cache/StaticCache.php');
require_once(dirname(__FILE__) . '/Parser/ParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/BotParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Bot.php');
require_once(dirname(__FILE__) . '/Parser/OperatingSystem.php');
require_once(dirname(__FILE__) . '/Parser/VendorFragment.php');
require_once(dirname(__FILE__) . '/Parser/Client/ClientParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Device/DeviceParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Yaml/Parser.php');
require_once(dirname(__FILE__) . '/Yaml/Spyc.php');
//Same as before, you'll need to find your own copy of spyc.php. Here is how I add it (pulls from a directory above the library):
require_once(realpath(dirname(__FILE__) . '/..') . '/spyc.php');
//Add as the first line of addClientParser():
require_once(dirname(__FILE__) . '/Parser/Client/' . $parser . '.php');
//Add as the first line of addDeviceParser():
require_once(dirname(__FILE__) . '/Parser/Device/' . $parser . '.php');

Categories