I know this has been answered many times, but I don't know why I'm getting this error:
Notice: Undefined variable: urlfilter in
/home/mjburkel/public_html/allxxcars/wp-content/plugins/insert-php/includes/shortcodes/shortcode-php.php(52)
: eval()'d code on line 75
I know that this would normally be if the variable is not defined yet, but I am declaring it, thus I'm not exactly sure what the issue is. here is my code..note this is php code that I'm inserting into a Wordpress PHP snippet plugin...so, not sure if that plugin is inserting some weird code or not reading it properly maybe?
<?php /* Template Name: Completed W517 */
error_reporting(E_ALL); // Turn on all errors, warnings and notices for easier debugging
// API request variables
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1'; // URL to call
$version = '1.0.0'; // API version supported by your application
$appid = 'XXX'; // Replace with your own AppID
$globalid = 'EBAY-US'; // Global ID of the eBay site you want to search (e.g., EBAY-DE)
$query = 'ford mustang'; // You may want to supply your own query
$safequery = urlencode($query); // Make the query URL-friendly
$i = '0'; // Initialize the item filter index to 0
// Create a PHP array of the item filters you want to use in your request
$filterarray =
array(
array(
'name' => 'MaxPrice',
'value' => '1000000',
'paramName' => 'Currency',
'paramValue' => 'USD'),
array(
'name' => 'MinPrice',
'value' => '100',
'paramName' => 'Currency',
'paramValue' => 'USD'),
array(
'name' => 'SoldItemsOnly',
'value' => 'true'),
);
function CurrencyFormat($number)
{
$decimalplaces = 2;
$decimalcharacter = '.';
$thousandseparater = ',';
return number_format($number,$decimalplaces,$decimalcharacter,$thousandseparater);
}
// Generates an indexed URL snippet from the array of item filters
function buildURLArray ($filterarray) {
global $urlfilter="";
global $i="";
// Iterate through each filter in the array
foreach($filterarray as $itemfilter) {
// Iterate through each key in the filter
foreach ($itemfilter as $key =>$value) {
if(is_array($value)) {
foreach($value as $j => $content) { // Index the key for each value
$urlfilter .= "&itemFilter($i).$key($j)=$content";
}
}
else {
if($value != "") {
$urlfilter .= "&itemFilter($i).$key=$value";
}
}
}
$i++;
}
return "$urlfilter";
} // End of buildURLArray function
// Build the indexed item filter URL snippet
buildURLArray($filterarray);
// Construct the findItemsByKeywords HTTP GET call
$apicall = "$endpoint?";
$apicall .= "OPERATION-NAME=findCompletedItems";
$apicall .= "&SERVICE-VERSION=$version";
$apicall .= "&SECURITY-APPNAME=$appid";
$apicall .= "&GLOBAL-ID=$globalid";
$apicall .= "&keywords=$safequery";
$apicall .= "&categoryId=213";
$apicall .= "&paginationInput.entriesPerPage=20";
$apicall .= "$urlfilter";
// Load the call and capture the document returned by eBay API
$resp = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($resp->ack == "Success") {
$results = '';
// If the response was loaded, parse it and build links
foreach($resp->searchResult->item as $item) {
if ($item->pictureURLLarge) {
$pic = $item->pictureURLLarge;
} else {
$pic = $item->galleryURL;
}
$link = $item->viewItemURL;
$title = $item->title;
$price = $item->sellingStatus->convertedCurrentPrice;
$bids = $item->sellingStatus->bidCount;
$end = $item->listingInfo->endTime;
$fixed = date('M-d-Y', strtotime($end));
if(empty($bids)){
$bids = 0;
}
// For each SearchResultItem node, build a link and append it to $results
$results .= "<div class=\"item\"><div class=\"ui small image\"><img height=\"200px\" width=\"130px\" src=\"$pic\"></div><div class=\"content\"><div class=\"header\">$title</div><div class=\"meta\" style=\"margin-top:.1em\"><span class=\"price\"></span><div class=\"extra\">Sold Date: $fixed</div><div class=\"extra\"><button class=\"ui button\"><b>Number of Bids:</b> $bids </button></div><div class=\"extra\"><button class=\"ui orange button\">Sold Price: $$price</button></div><div class=\"description\"></div></div></div></div>";
}
}
// If the response does not indicate 'Success,' print an error
else {
$results = "<h3>Oops! The request was not successful. Make sure you are using a valid ";
$results .= "AppID for the Production environment.</h3>";
}
?>
What version of PHP are you using? You should actually be getting a syntax error when you first declare $urlfilter in your buildURLArray() function:
php: error - unexpected '=', expecting ',' or ';' in Standard input code
Because you're not supposed to be defining a variable after invoking it with the global keyword.
Basically, you should replace the beginning of your function with this:
// Generates an indexed URL snippet from the array of item filters
function buildURLArray ($filterarray) {
global $urlfilter, $i; // Load global variables
//…
}
And $urlfilter should be defined in the global scope, not the function scope. You could probably just put it under $i = '0'; // Initialize the item filter index to 0
Related
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 (terrible) code:
// 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(
'aaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbb'
);
$session->requestCredentialsToken();
$accessToken = $session->getAccessToken();
$str = $_POST['str'];
$str_length = strlen($_POST['str']);
$html = null;
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);
$search = $api->search('fugazi','artist');
$search_array = objectToArray($search);
r($search_array);
// loop through the results
foreach($search_array['artists']['items'] as $item) {
// artist name
$artist_name = $item['name'];
$html .= "<h2>$artist_name</h2>";
// genres
foreach($item['genres'] as $genre) {
$html .= " <span class='code'>$genre</span> ";
}
// $v2 = $item['images']['0']['height'];
// r($v2);
}
This is what the php-ref shows the $search_array to look like:
Or as plain text: https://0bin.net/paste/iPYfn2M6#Pt8l-a9i9el0TVnZsDTA8wlbe/t0CGSIsp4bds0IZyT
I would like to access the attributes for the first element in the images array for each artist returned.
I tried via this line:
$v2 = $item['images']['0']['height'];
But the following error was returned:
PHP Notice: Undefined offset: 0 in C:\Websites\spotify\search.php on line 68
PHP Notice: Trying to access array offset on value of type null in C:\Websites\spotify\search.php on line 68
I have searched around that error but I can't get my head around the correct syntax to use in this example.
Sorry for my lack of understanding.
Looking for help in counting all values that are present in HTTP API. The link below outputs the values of multiple data points by date. What I am trying to achieve is a count on each value so that instead of
a:2:{s:10:"2018-01-03";a:9:{s:12:"nb_pageviews";d:2031;}s:10:"2018-01-04";a:9:{s:12:"nb_pageviews";d:25;}
I want to output the above nb_pageviews as a key with the value of the dates combined - 2056.
Example Link for API is https://demo.piwik.org/?module=API&method=Actions.get&idSite=7&period=day&date=last2&format=php&token_auth=anonymous
And the PHP to display this is
$url = "https://demo.piwik.org/";
$url .= "?module=API&method=Actions.get";
$url .= "&idSite=7&period=day&date=last2";
$url .= "&format=php";
$url .= "&token_auth=anonymous";
$fetched = file_get_contents($url);
$content = unserialize($fetched);
// case error
if (!$content) {
print("NO DATA");
}
foreach ($content as $row) {
$pageviews = $row['nb_pageviews'];
print("<div>$pageviews</div>\n");
}
Please note that the above link contains multiple other values that I will be wanting to do the same with, yet for ensuring the readability of this question I have kept the values simple and to just the one.
Just use the combined assignment operator, and be sure to set defaults of 0, before the loop.
$fetched = 'a:2:{s:10:"2018-01-03";a:9:{s:12:"nb_pageviews";d:2031;s:17:"nb_uniq_pageviews";d:964;s:12:"nb_downloads";d:2;s:17:"nb_uniq_downloads";d:2;s:11:"nb_outlinks";d:68;s:16:"nb_uniq_outlinks";d:64;s:11:"nb_searches";d:33;s:11:"nb_keywords";d:16;s:19:"avg_time_generation";d:0.78600000000000003;}s:10:"2018-01-04";a:9:{s:12:"nb_pageviews";d:25;s:17:"nb_uniq_pageviews";d:10;s:12:"nb_downloads";i:0;s:17:"nb_uniq_downloads";i:0;s:11:"nb_outlinks";d:1;s:16:"nb_uniq_outlinks";d:1;s:11:"nb_searches";d:2;s:11:"nb_keywords";d:2;s:19:"avg_time_generation";d:0.79300000000000004;}}';
$content = unserialize($fetched);
// case error
if (!$content) {
print("NO DATA");
}
$pageviews = 0;
$uniq_pageviews = 0;
// and on, with your other vars you're looking to sum...
foreach ($content as $row) {
$pageviews += $row['nb_pageviews'];
$uniq_pageviews += $row['nb_uniq_pageviews'];
}
var_export(['pageviews' => $pageviews, 'uniq_pageviews' => $uniq_pageviews]);
Working demo: https://eval.in/930183
// Output
array (
'pageviews' => 2056.0,
'uniq_pageviews' => 974.0,
)
To print these values, you might replace the var_export line with something like this:
$data = [
'pageviews' => $pageviews,
'uniq_pageviews' => $uniq_pageviews
];
Which you could output in some HTML like so:
<div class="col-md-4"><?php echo $data['pageviews']; ?></div>
<div class="col-md-4"><?php echo $data['uniq_pageviews']; ?></div>
ok below is my code
<?php
// Last 10 Jobs
function last10IT(){
$xml = simplexml_load_file('http://www.cv-library.co.uk/cgi-bin/feed.xml?affid=101899');
$new_array = array();
//$limit = 5;
//$c = 0;
foreach ($xml->jobs->job as $job) {
// if ($limit == $c) {
// break;
// }
$jobref = $job->jobref;
$title = $job->title;
$date = $job->date;
$new_array[$jobref.$date] = array(
'jobref' => $jobref,
'date' => $date,
'title' => $title,
'salary' => $job->salary,
'location' => $job->location,
);
}
}
ksort($new_array);
$showl = 10;
$n = 0;
foreach ($new_array as $date => $listing) {
print $listing['title'] . PHP_EOL;
}
?>
All I want it to do is filter by category & display a max of 10 results
for example
IT
so is there a way I can pass the category value into the function that I want it to filter by
instead of having to replicate for each category
All I get is :
Warning: ksort() expects parameter 1 to be array, null given in
C:\wamp\www\RECRUITMENTFAIR\functions.php on line 28
Please help guys
It something SO simple causing this error but its driving me mad because I just cannot see it
it is relative simple: you try to use the variable $new_array out of scope:
it is defined within your last10IT() function, but the function ends after the first foreach.
you should either return the array and call the function to get the array or move the part with the ksort and the printing into the function, depending on your needs.
Also was having issues by not having the PHP Extension xmlrpc enabled !
what a tool !
Thats why i was getting failed to open half the time
Any reason why i'm getting this error
PHP Notice: Undefined index: url on line 40
From my code the code is used to grab link for movies from a site but as i try to grab the link i get that error so could someone point me in the right direction on solving the issue.
public function getMovieEmbeds($title) {
$misc = new Misc();
//Step1 find key
$movie_url = null;
$html = file_get_html('http://www.primewire.ag/');
$elements = $html->find('input[name=key]',0);
$key = null;
if(!is_null($elements)){
$key = $elements->value;
}
if(is_null($key)){
return array();
}
//Step2 do search...
$search = urlencode($title);
$html = file_get_html("http://www.primewire.ag/index.php?search_keywords=$search&key=$key&search_section=1");
$elements = $html->find(".index_item h2");
if(!is_null($elements)){
foreach($elements as $element){
$element_title = strtolower(strip_tags(trim(preg_replace('/\s*\([^)]*\)/', '', $element->innertext))));
if ($element_title == strtolower(trim($title))) {
$parent = $element->parent();
$movie_url = "http://primewire.ag".$parent->href;
break;
}
}
}
if (is_null($movie_url)) {
return array();
}
//Step3 get embeds
$html = file_get_html($movie_url);
$elements = $html->find(".movie_version_link a");
if(!is_null($elements)){
foreach($elements as $element){
$encoded_url = "http://primewire.ag".$element->href;
$query = parse_url($encoded_url, PHP_URL_QUERY);
parse_str($query,$op);
error came from ---> $link = base64_decode($op["url"]); <--- here
if(strpos($link, "affbuzzads")===false && strpos($link, "webtrackerplus")===false){
$embed = $misc->buildEmbed($link);
if ($embed) {
$embeds[] = array(
"embed" => $embed,
"link" => $link,
"language" => "ENG",
);
}
}
}
return $embeds;
}
return array();
}
could some one please help me it would be most helpful
parse_str returns an array of the parameters on a URL, as if it was in the $_GET array.
It would only contain a 'url' value if the url you used had a parameter 'url'
E.g. it was along the lines of:
http://example.com/?url=urlvalue
Therefore, if $element->href (and therefore $encoded_url and $query) does not contain a parameter named 'url' in it you'll not get an index in the $op array of 'url'
See the documentation here: http://us1.php.net/manual/en/function.parse-str.php
I am using user table from fql.I am passing array to json_encode(). Facebook json format is not corrct, it is excluding []. How do I add this in my code.
Below code is a example but I do not under stand that code.
user table https://developers.facebook.com/docs/reference/fql/user
I want name,uid.
//to get album cover
$fql2 = "select src from photo where pid = '" . $values['cover_pid'] . "'";
$param2 = array(
'method' => 'fql.query',
'query' => $fql2,
'callback' => ''
);
$fqlResult2 = $facebook->api($param2);
$jsarr = array();
foreach( $fqlResult2 as $keys2 => $values2){
}
if ($values['name'] != 'Profile Pictures'){
$jsarr['src'] = $album['src'];
$count += 1;
if ($count == 1){
$outputStr .= "[";}
else {
$outputStr .= ",";}
$outputStr .= json_encode($values2);
}
}
$outputStr .= "]";
$outputStr = str_replace("{","[",$outputStr);
$outputStr = str_replace("}","]",$outputStr);
echo $outputStr;
}
?>
When you use the Facebook PHP SDK, it automatically decodes the returned json object into a PHP array. If you need a json object from it, you'll have to use json_encode on it after you finish processing it.
You've got some major problems with your php code. Just to get started:
Your foreach loop ends as soon as it starts with the } on the next line.
Where do you define the variables $values and $album?
You are referencing $values2 outside of your foreach loop.
You are appending to an uninitialized variable on your line $outputStr .= "[";.
If you pass json_encode() a proper php array of data, it will make a complete json object. You shouldn't need to be appending brackets of any kind.