Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-07-30 22:45:15

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Error 406 Not Acceptable

Please take a look at this PHP code. I am trying to retrieve data via two different API’s and sum the retrieved values to get a total result. The code works fine if I put it into a file on the server and navigate to /file.php. You can check it here. It returns a 406 error if I put it into a Textpattern page template and try to save it. Working on Txp 4.5.7. Can you please help me understand why Textpattern refuses to accept this code? Thanks.

<txp:php>
function get_show_stat($show_id) {
$url_1 = ‘https://api.spreaker.com/v2/shows/’ . $show_id . ‘/statistics?oauth2_access_token=XXXX’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_1);
//curl_setopt($ch, CURLOPT_GET, count($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result);
}
$show_id = XXXX;
$result = get_show_stat($show_id);
if (array_key_exists(‘error’, $result->response)) { echo $result->response->error->messages0;
} else { $plays_count = $result->response->statistics->plays_count; $downloads_count = $result->response->statistics->downloads_count; $total = $plays_count + $downloads_count;
}
$url_2 = ‘https://www.podbean.com/api2/public/filesPlays?blogId=XXXX&query%5B0%5D%5Bfile%5D=allFile&query%5B0%5D%5Bw%5D=XXXX’;
$json = file_get_contents ($url_2);
$string = json_decode($json);
$allFile = $string->data->allFile;
$grand_total = $allFile + $total;
$formattedNum = number_format($grand_total);
echo $formattedNum;
</txp:php>

EDIT: obfuscated your access codes and query and show ids.

Last edited by gilibaus (2019-07-31 08:01:40)

Offline

#2 2019-07-31 08:06:25

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Error 406 Not Acceptable

For me it returns

223,379


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2019-07-31 08:20:25

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Re: Error 406 Not Acceptable

@colak, thanks for trying. You are loading file.php via the link I provided and it works fine. But when I try to copy/paste the same PHP code of file.php into a page on the admin side of Textpattern, I get a 406 error upon saving the page. Why?

Offline

#4 2019-07-31 08:26:53

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Error 406 Not Acceptable

Do you have Allow PHP in pages set to yes, in Admin>preferences>Publish?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#5 2019-07-31 08:45:25

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Re: Error 406 Not Acceptable

Yes, both in pages and articles

Offline

#6 2019-07-31 09:58:56

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Re: Error 406 Not Acceptable

I have noticed that removing the curl lines from the code it obviously breaks but at that point Textpattern saves the page normally. The server has curl enabled.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_1);
//curl_setopt($ch, CURLOPT_GET, count($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

Offline

#7 2019-07-31 10:16:11

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Re: Error 406 Not Acceptable

I have also tried this solution via htaccess file, but to no avail.

Offline

#8 2019-07-31 10:23:27

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Error 406 Not Acceptable

How about you put the entire code snippet into a form get_show_stats and then in your article body, use:

<txp:output_form form="get_show_stats" />

TXP Builders – finely-crafted code, design and txp

Offline

#9 2019-07-31 10:53:58

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Re: Error 406 Not Acceptable

@jakob thanks for your suggestion. Tried to save the form and got the same 406 error :(

Offline

#10 2019-07-31 12:45:21

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,269
Website GitHub

Re: Error 406 Not Acceptable

Sounds like your host’s counter-spam measures are being tripped by something in that content. I would get in touch with them, sending them the code you’re trying to save, and ask if there’s anything they can do.

In the meantime, as well as the things you tried above, please scan this thread and pay attention to Robert’s posts, substituting 406 where he mentions 403. Might at least give some more info behind the reason for this behaviour that you can report to your host.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#11 2019-07-31 12:49:25

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Error 406 Not Acceptable

gilibaus wrote #318899:

@jakob thanks for your suggestion. Tried to save the form and got the same 406 error :(

Right. That does sound like maybe your host is filtering out such code. You’re on Textpattern 4.7+? How about:

  • Export your current theme to disk from the Presentation › Themes panel so that you have the most recent version on your server as files.
  • Upload your <txp:php>…</txp:php> code as a file called get_show_stats.txp in the /themes/your-theme/forms/misc/ folder.
  • From the Presentation › Themes panel, update/import the files into your database. I’m hoping that the host won’t scan that during the import procedure.
  • Now try the output_form in your article body:
<txp:output_form form="get_show_stats" />

TXP Builders – finely-crafted code, design and txp

Offline

#12 2019-07-31 14:27:17

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 91
Website

Re: Error 406 Not Acceptable

Thank you. Unfortunately I am on Textpattern 4.5.7 but after reading your suggestion a light bulb turned on in my head. I created the form directly in the database via phpMyadmin, and it worked. Filter bypassed. Now there is a new issue though: when I load the file called file.php, which is a file I use to test the php code, it works fine and the resulting number updates (if that’s the case). The same code but in the get_show_stats form does NOT update the resulting number when I refresh the page the form is inserted into. Any idea why?

Last edited by gilibaus (2019-07-31 14:32:03)

Offline

Board footer

Powered by FluxBB