Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

gilibaus
Member
From: Milan, Italy
Registered: 2013-08-14
Posts: 67
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: 7,325
Website

Re: Error 406 Not Acceptable

For me it returns

223,379


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | Respbublika! | NeMe @ github

Offline

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

gilibaus
Member
From: Milan, Italy
Registered: 2013-08-14
Posts: 67
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: 7,325
Website

Re: Error 406 Not Acceptable

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


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | Respbublika! | NeMe @ github

Offline

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

gilibaus
Member
From: Milan, Italy
Registered: 2013-08-14
Posts: 67
Website

Re: Error 406 Not Acceptable

Yes, both in pages and articles

Offline

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

gilibaus
Member
From: Milan, Italy
Registered: 2013-08-14
Posts: 67
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: Milan, Italy
Registered: 2013-08-14
Posts: 67
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: 3,522
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: Milan, Italy
Registered: 2013-08-14
Posts: 67
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: 8,766
Website

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

Board footer

Powered by FluxBB