Community @ The Turning Gate

Support community for TTG plugins and products.

NOTICE

The Turning Gate's Community has moved to a new home, at https://discourse.theturninggate.net.

This forum is now closed, and exists here as a read-only archive.

  • New user registrations are disabled.
  • Users cannot create new topics.
  • Users cannot reply to existing topics.

You are not logged in.

#26 2020-05-12 13:55:00

Nico3939
Member
From: France
Registered: 2016-10-05
Posts: 235
Website

Re: Getting Started with new JSON API

Hi Daniel, thank you very much for your help!


I completely deleted my custom php code to leave only the one you send me, so here is my full PHP file and the error returned:

<?php
/*
 *  TTG Core Elements "PHPlugins" User Hooks v1.2 - initialization mainline
 *
 *  developed by john bishop images (http://johnbishopimages.com)
 *  for Matthew Campagna of The Turning Gate (http://theturninggate.net)
 *
 */

function user_load($style, $path) {
  $g_tsvrl = explode(' ', $style); // Extract gallery type
  define ('G_STYLE', strtoupper($g_tsvrl[1])); // and set global for later
  $g_path = str_ireplace('\\','/',$path); // change \ to /
  $chunks = explode('/',$g_path); // and put into array
  define ('G_PATH', strtoupper($chunks[count($chunks)-2])); // gallery folder name is second to last
  //define ( 'TTG_SITE', ''); // set new site root for navigation, resources, etc.
}

if (defined('BACKLIGHT_HOOK')) {
	require_once(realpath(BACKLIGHT_HOOK).'/modules/module-designer/application/helpers/APHPlugins.php');
}

class PHPlugins extends APHPlugins
{
// Returns the JSON API response as an array. If an error was detected
// an error response is returned
//	
// Error response:
// (
//     ['status'] => 'error'
//     ['code'] => error number
//     ['message'] => error description
// )
function dlp_get_wp_rest_response($url){
	
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_URL => $url,
		CURLOPT_USERAGENT => 'User Agent X',
		CURLOPT_FAILONERROR => true
	));
	$posts = curl_exec($curl);

	// Was an error detected?
	if(curl_errno($curl)>0){	 
		$posts = Array();
		$posts['status'] = 'error';
		$posts['code'] = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
		$posts['message'] = curl_errno($curl).': '.curl_error($curl);
	} else {
		$posts = json_decode($posts, true);
	}

	curl_close($curl);
	return $posts;
}

function footer_bottom() {
	$posts = $this->dlp_get_wp_rest_response("https://www.phototheque.nicolaslogerot.com/backlight/api/get_album/343971");
	
	// check for errors 
	if (array_key_exists('status', $posts)){
		// error detected:
		echo '<pre>';
		print_r($posts);
		echo '</pre>';
	} else {
		echo "Array length: " . count($posts['album']['photos']);
	}
}
}   
?>

Something went wrong
array_key_exists() expects parameter 2 to be array, null given in perso-home.php on line 63


Line 63 is:

	if (array_key_exists('status', $posts))

Offline

#27 2020-05-12 14:51:51

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,624
Website

Re: Getting Started with new JSON API

Hi Nico, let's do some more debugging:

function dlp_get_wp_rest_response($url){
	
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_URL => $url,
		CURLOPT_USERAGENT => 'User Agent X',
		CURLOPT_FAILONERROR => true
	));
	$posts = curl_exec($curl);

	echo '<pre>';
	print_r($posts);
	echo '</pre>';
}

function footer_bottom() {
	$posts = $this->dlp_get_wp_rest_response("https://www.phototheque.nicolaslogerot.com/backlight/api/get_album/343971");
}

Daniel Leu | Photography   
DanielLeu.com
My digital playground (eg, Backlight tips&tricks): lab.DanielLeu.com

Offline

#28 2020-05-12 15:59:44

Nico3939
Member
From: France
Registered: 2016-10-05
Posts: 235
Website

Re: Getting Started with new JSON API

Daniel you put me on the right way!

Now, I have this message:

The document has moved here.

I think the error comes because I put redirection on my web site in htaccess:

RewriteCond %{HTTPS} off [OR]
	RewriteCond %{HTTP_HOST} ^www\. [NC]
	RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
	RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
	
	RewriteCond %{HTTP_HOST} ^phototheque.nicolaslogerot.com$ [NC]
	RewriteCond %{REQUEST_URI} ^/$
	RewriteRule ^(.*)$ /galleries/stock/ [L]

and in index.php in my website.com/galleries/index.php

header('Location:  ../');

Without all of this redirection, I have now this message:

Array
(
    [status] => error
    ['code'] => 403
    [message] => 22: The requested URL returned error: 403 
)

In fact the first redirection, is an invisible redirection for user:

"https://phototheque.nicolaslogerot.com/galleries/stock" to "https://phototheque.nicolaslogerot.com/"

and the second redirection is to prevent google users and robots from referencing the page:

"https://phototheque.nicolaslogerot.com/galleries/"

Otherwise it returns to an empty page.



My backlight installation from lightroom contains only one gallery named: "Stock" which is my home page.

To avoid having the home page with a long address like
"https://phototheque.nicolaslogerot.com/galleries/stock"

I made an invisible redirect in order to obtain a simplified address like:
"https://phototheque.nicolaslogerot.com/"

Here, I hope to be quite clear, the explanations are not simple and I am French and my English is limited.

Thank you very much Daniel!

Offline

#29 2020-05-13 00:55:15

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,624
Website

Re: Getting Started with new JSON API

Thank you, Nico. Still something strange is going on. 403 is an http 'forbidden' error code. Can you try a stock .htaccess file? You can find it in the Backlight installer download.

It looks like that all your .htaccess magic locks yourself out while I can run my script accessing data from your server!


Daniel Leu | Photography   
DanielLeu.com
My digital playground (eg, Backlight tips&tricks): lab.DanielLeu.com

Offline

#30 2020-05-13 22:04:37

Nico3939
Member
From: France
Registered: 2016-10-05
Posts: 235
Website

Re: Getting Started with new JSON API

When I put default .htaccess file in my Backlight installation, it put the default page (index page: yes), with the album rendering all image on the same page, and it take very loooonnggg time to load

Last edited by Nico3939 (2020-05-13 22:06:02)

Offline

#31 2020-05-14 00:39:44

Nico3939
Member
From: France
Registered: 2016-10-05
Posts: 235
Website

Re: Getting Started with new JSON API

But if I disable album or I change with another index page without album, nothing append too

Last edited by Nico3939 (2020-05-14 00:40:08)

Offline

#32 2020-05-17 21:44:38

Markus
Member
From: Witten - Germany
Registered: 2012-10-06
Posts: 204
Website

Re: Getting Started with new JSON API

Daniel Leu wrote:

I'm running this code, Nicolas, and it works without problems:

function footer_bottom() {
   $posts = $this->dlp_get_wp_rest_response("https://www.phototheque.nicolaslogerot.com/backlight/api/get_album/343971");
   echo "Array length: " . count($posts['album']['photos']);
}

I'm surprised that even performance doesn't seem to be an issue when doing this over the internet. It will be even faster on your machine.

Hello everyone, Daniel's code seems to work for me - thanks for that.
On my test page bl3.mc-photografie.de the number of photos of the given gallery is displayed on each page above the footer.

What I ask myself now? Is it also possible to use such a code, e.g. to display the number of photos above the galleries on every gallery page?

"This gallery contains x photos"

Sunny greetings
Markus

Offline

#33 2020-05-17 22:04:43

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Getting Started with new JSON API

should be. just use the appropriate hook. See the phplugins file for a list of them all.


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#34 2020-05-17 22:45:20

Markus
Member
From: Witten - Germany
Registered: 2012-10-06
Posts: 204
Website

Re: Getting Started with new JSON API

Hi Rod,

i have tested the following phplugins hooks:

album_top and album_bottom insteat of footer_top or footer_botton, which I assume are the right ones.

The following error message appears for both:

Something went wrong

Unexpected error: Cannot redeclare PHPlugins::album_bottom() in phplugins-mc-photografie.php on line 423

Please report error at http://community.theturninggate.net

Greetings, Markus

Offline

#35 2020-05-17 23:05:30

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Getting Started with new JSON API

were you already using the album_bottom hook for something?


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#36 2020-05-17 23:36:10

Markus
Member
From: Witten - Germany
Registered: 2012-10-06
Posts: 204
Website

Re: Getting Started with new JSON API

ok, I see sad

I use the album_top hook to display the found photos in the photo search.
And I use the album_bottom hook for the "back-link" to the previous album

Then I guess this probably won't work?

Offline

#37 2020-05-18 00:06:42

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Getting Started with new JSON API

you can add the code to either hook, along with your existing code. you just can’t call a hook more than once.


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#38 2020-05-18 09:29:59

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,624
Website

Re: Getting Started with new JSON API

For those following this thread and are running in some errors with the original code, I have improved the JSON fetching function with some additional error handling. More on my blog at http://lab.danielleu.com/blog/using-bac … hp-take-2/


Daniel Leu | Photography   
DanielLeu.com
My digital playground (eg, Backlight tips&tricks): lab.DanielLeu.com

Offline

#39 2020-05-18 09:37:35

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,624
Website

Re: Getting Started with new JSON API

Markus wrote:

ok, I see sad

I use the album_top hook to display the found photos in the photo search.
And I use the album_bottom hook for the "back-link" to the previous album

Then I guess this probably won't work?

You can use something like

function album_top(){
    if ($this->dlp_page_match('/backlight/search')){
      echo ' whatever you want to display on search albums';
   } else {
      echo ' whatever you want to display on regular albums';
   }
   return true;
}

The 'dlp_page_match' function is described over on my blog: https://lab.danielleu.com/blog/page-specific-php-code/


Daniel Leu | Photography   
DanielLeu.com
My digital playground (eg, Backlight tips&tricks): lab.DanielLeu.com

Offline

#40 2020-05-18 13:40:17

Markus
Member
From: Witten - Germany
Registered: 2012-10-06
Posts: 204
Website

Re: Getting Started with new JSON API

Daniel Leu wrote:

You can use something like

function album_top(){
    if ($this->dlp_page_match('/backlight/search')){
      echo ' whatever you want to display on search albums';
   } else {
      echo ' whatever you want to display on regular albums';
   }
   return true;
}

The 'dlp_page_match' function is described over on my blog: https://lab.danielleu.com/blog/page-specific-php-code/

Hi Daniel,
I'm gonna have to take another look at that.
Thanks, Markus

Offline

Board footer

Powered by FluxBB