I am using jwilsson's spotify-web-api-php to return data from the Spotify API using PHP.
Also I'm using digitalnature's php-ref to return info about variables.
This is my code, apologies for the fact it's probably full of errors, misconceptions, rubbish etc.
// https://stackoverflow.com/questions/4345554/convert-a-php-object-to-an-associative-array
function objectToArray($r)
{
if (is_object($r)) {
if (method_exists($r, 'toArray')) {
return $r->toArray(); // returns result directly
} else {
$r = get_object_vars($r);
}
}
if (is_array($r)) {
$r = array_map(__FUNCTION__, $r); // recursive function call
}
return $r;
}
$session = new SpotifyWebAPI\Session(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
);
$session->requestCredentialsToken();
$accessToken = $session->getAccessToken();
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);
$search = $api->search('fugazi','artist');
// convert object to array
$fff = objectToArray($search);
// php-ref to view info about the $fff array
r($fff);
// try to access the total value
$v1 = $fff[0]['artists']['total'];
This is what the php-ref looks like for the $fff variable:
I tried to access the total attribute via:
$v1 = $fff[0]['artists']['total'];
But receive these errors:
PHP Notice: Undefined offset: 0 in C:\websites\spotify\search.php on line 49
PHP Notice: Trying to access array offset on value of type null in C:\websites\spotify\search.php on line 49
PHP Notice: Trying to access array offset on value of type null in C:\websites\spotify\search.php on line 49
Sorry for asking a silly question and for not understanding what I'm doing, but, what am I doing wrong?
Related
I am using my application in a shared hosting, it is produced in laravel. It gives me
PHP Warning: array_shift() expects parameter 1 to be array, null given in line 53
PHP Notice: Undefined index: argv in line 49
with this code:
class ArgvInput extends Input
{
private $tokens;
private $parsed;
public function __construct(array $argv = null, InputDefinition $definition = null)
{
if (null === $argv) {
$argv = $_SERVER['argv'];
}
// strip the application name
array_shift($argv);
$this->tokens = $argv;
parent::__construct($definition);
}
It can mean that the $_SERVER['argv'] variable is also empty. I'd recommend to check it first, or you can do this:
$argv = $argv ?? $_SERVER['argv'] ?? [];
This way you'll always have an array, even if there's no input, in which case the array will be empty, and it won't trigger that error
I have this piece of code:
private function _resolveCustomEntries($fields)
{
foreach ($fields as &$value) {
if (array_key_exists('custom', $value)) {
if (method_exists($this, $value['custom'])) {
$value['custom'] = $this->$value['custom'](); //variableInterpolation
}
}
}
return $fields;
}
I ran a PHP 7.2 compatibility check and it complained here with the "variableInterpolation" on the marked line. When I run this code, the PHP log tells me this:
ERR (3): Notice: Array to string conversion in
/public_html/lib/KiTT/Payment/Widget.php on line 217
Thats the same line where the "variableInterpolation" check failed. So how would I rewrite this code so it works in PHP 7.2?
Thanks!
Solution:
$value['custom'] = $this->$value['custom']();
has to look like this:
$value['custom'] = $this->{$value['custom']}();
It's a matter of order variables are evaled.
With
class x {
public function y() {
echo 'ok';
}
}
$x = new x();
$y = array('i' => 'y');
Then
$x->$y['i']();
Fails because PHP first tries to cast the $y variable into a string, and get the matching property of $x (which btw does not exist), then tries to get the index 'i' or that unexisting property, and then tries to run it as a callable.
Hence 3 errors:
Array to string conversion
Undefined property x::$Array
Function name must be a string (nda: the undefined property returns NULL)
Instead, curly brace the variable to set the resolving order:
$x->$y['i']();
Will work. So use $this->{$value['custom']}()
This will throw an array to string conversion in 7.2
class bob{
function foo(){
return 'bar';
}
function getFoo(){
$value['custom'] = 'foo';
$value['custom'] = $this->$value['custom']();
return $value['custom'];
}
}
$bob = new Bob();
var_dump($bob->getFoo());
But it will execute just fine in 5.6.
Then i changed the snippet to this, not calling the method directly casting the array key to function name, but initializing a string (hopefully, there is no type validation in your code) variable with the function name first:
class bob{
function foo(){
return 'bar';
}
function getFoo(){
$value['custom'] = 'foo';
$functionName = $value['custom'];
$value['custom'] = $this->$functionName();
return $value['custom'];
}
}
$bob = new Bob();
var_dump($bob->getFoo());
This will run just fine in php 7.2
You could try rewriting your code using complex (curly) syntax, you can read more about it here.
Your code would look something like this.
$value['custom'] = $this->{$value['custom']}();
Edit: moved the curly braces to correct positions.
function drupal_build_css_cache($css) {
$data = '';
$uri = '';
$map = variable_get('drupal_css_cache_files', array());
// Create a new array so that only the file names are used to create the hash.
// This prevents new aggregates from being created unnecessarily.
$css_data = array();
foreach ($css as $css_file) {
$css_data[] = $css_file['data'];
}
PHP Notice: Undefined index: data on line 3606.
Line 3606 being: $css_data[] = $css_file['data'];
To avoid this error just test if the table fields was initialized with isset () .
// Before using $css_file['data'];
if (isset($css_file['data']))
{
$css_data[] = $css_file['data'];
}
It is just notice, that you have undefined index; you should first check if is it set:
if (isset($css_file['data'])) {
$css_data[] = $css_file['data'];
}
Otherwise you can turn off notice reporting (put this line at the beginning of your code):
error_reporting(E_ALL ^ E_NOTICE);
PHP notice is telling you that $css_file has no key data. Check what you receive in $css variable.
First, you should show what you've tried to fix the issue. As the error shows, it's caused because $css_file['data'] is undefined. You haven't shown us where $css_file is defined. $css_file['data'] is most likely not being defined.
I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:
Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36
Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36
in the game_header.php file I have
$user = new User($_SESSION['id']);
And thought it would work, but sadly it does not.
I have also tried
$user = new User($_SESSION['id']);
on the edit_account.php page, but I was getting the same error.
Here's the edit_account.php code.
Does anyone know what I might be doing wrong here?
include "game_header.php";
$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';
switch($_GET['type']) {
case 'profileoptions' : profile_options(); break;
default : profile_options();
}
function profile_options() {
echo '
';
include 'game_footer.php';
}
When encased into a function you must do the following:
global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.
So it must start with:
function profile_options() {
global $user ;
//The rest of code
}
However, I recommend you to create a parameter:
function profile_options(User $user){
//Much code
}
Then call it where $user is accessible:
profile_options($user) ;
i want to display informations about object Activity that the function getCurrent() from the ListActivity should returns.
When i try it, it works perfectly, i have the information needed from the class, but, i have this error message on the top of the page :
Fatal error: Call to a member function getIdentifiant() on a
non-object in
/Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php
on line 34
Line 34 is here :
while($listActivities->next())
{
$current = new Activity();
$current = $listActivities->getCurrent();
echo $current->getId(); // line 34
}
And this is the getCurrent() function which return an Activity object.
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
}
I don't understand why i have this problem since it returns me the object that i want.
Please help me figuring it out. Thanks.
echo $current->getId(); // line 34
Fatal error: Call to a member function getIdentifiant() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php on line 34
WhatEVER you think happens, or whatEVER you see in your page, if the error says $current is not an object, it is not. It might not be null, but it could also be an array or anything that is not an object.
Also :
$current = new Activity();
$current = $listActivities->getCurrent();
Doesn't really makes sense to me.
Use
var_dump($listActivities->getCurrent());
To see what it exactly returns, and trust what errors say.
EDIT : And you may not even be looking at the right php script acually : The error says "getIdentifiant" while the code says "getId". Make sure you're looking at the right piece of code and refreshing the right page.
first set $this->current after $current = new Activity(); (maybe in constercture)
and you should return false if !isset($this->activities[$this->current])
also if you use $current = $listActivities->getCurrent() you lost your Activity object , it should save into another variable
here new code :
while($listActivities->next())
{
$current = new Activity();
if( $listActivities->getCurrent() )
echo $current->getId(); // line 34
}
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
return false;
}