Urlfetch returns more characters than it should

Hi,

I wrote a simple PHP script and uploaded it to a website hosted on epizy. The script itself just generates a short string and echoes it. It was my understanding based on a youtube tutorial that if I called urlfetch in a Nightbot command targeting that webpage, then the script would be ran, the text would be generated and it would be sent as a response to the urlfetch, which would then display it in Twitch chat. However, it keeps telling me that the response is longer than 400 characters, even with a test script that’s literally only this:

<?php echo "Test."; ?>

This is not super important, but I’d try to get it to work properly in the near future anyway. Can I get some help troubleshooting this please? I can’t really find any documentation on what’s going on behind the scenes, so I don’t have a lot to go on :frowning:

Thanks!

Hey @zoliking!

You can start to get an idea of what’s going on with the following command:

!addcom !test $(eval a=`$(urlfetch json URL)`;a.slice(0,400))

Or if you want to get the full message, the following can work too:
First measure the length of the reply this way:

!addcom !length $(eval a=`$(urlfetch json URL)`; a.length)

Then, let’s say the length is 2357, the following code should help you retrieve the full message:

!addcom !test $(eval a=`$(urlfetch json URL)`; $(urlfetch http://rokbot.xyz/smm.php?msg=${a.slice(0,400)}`${a.slice(400,800)}`${a.slice(800,1200)}`${a.slice(1200,1600)}`${a.slice(1600,2000)}`${a.slice(2000)}&i=5&d=1))

Note: I’m not 100% sure it’ll work, might need to tweak the code a bit.
Edit: as expected, it doesn’t work, check my next reply for a working code.

If it’s longer than 2400 characters don’t forget to add an ending parameter to the last .slice(), and you’ll have to get the full message in few commands.
If it’s shorter, update the second $(urlfetch) to your needs.


Here’s the second $(urlfetch) I used when empty, the messages separation is ` (backticks), it supports a maximum of 6 messages:

$(urlfetch http://rokbot.xyz/smm.php?msg=MESSAGE_1`MESSAGE_2`MESSAGE_3`MESSAGE_4`MESSAGE_5`MESSAGE_6&i=5&d=1)

Thank you for the quick answer. The first one just gave me an “Error connecting to remote server.” The second one stated “Right hand side of instanceof is not an object”, which is odd, because “instanceof” is not part of any of the code I’m running. :confused:

Right hand side of instanceof is not an object is a common Nightbot error.
And as I said I was doubting the code I’ve given would work, I had a quick look into it, and here’s a solution that works:

!addcom !test $(urlfetch http://rokbot.xyz/smm.php?msg=$(eval "$(urlfetch json URL)".slice(0,400))`$(eval "$(urlfetch json URL)".slice(400,800))`$(eval "$(urlfetch json URL)".slice(800,1200))`$(eval "$(urlfetch json URL)".slice(1200,1600))`$(eval "$(urlfetch json URL)".slice(1600,2000))`$(eval "$(urlfetch json URL)".slice(2000,2400))&i=5&d=1)

I hope your URL is short enough so it fits under the 500 character limit Twitch has.

Now if !length gives you an Error connecting to remote server, the issue is likely on your server side. Or have you made sure to replace URL in my code with yours?

I replaced the URL, yes :). With the new code it’s Error connecting to remote server a few times then Error connecting to webeval service. If it’s a server side issue I can’t do much, I just started an account on a free webhosting service and uploaded the files there. :confused: At least when I open the site in a browser, it works, I know that much.

Hmm, to me it looks like it’s a server issue then, because I did a test with a service I know to be working and I don’t get the error, I’m afraid I can’t help you more than this, I’m sorry, but maybe the others providing help will have more insight than me on the matter.

Thank you so much for the help anyway, I appreciate it a lot!

1 Like

Hiya, without knowing the url you’re trying to fetch it’s hard to say what’s wrong, however a free webhost sometimes injects some extra code (for tracking for example) in your page.
You check check the page-source in the browser, to see if it’s just plain-text with no html tags. Or post the url here so I can test it.

1 Like

Thanks for the response, I’ll check it out. The website isn’t a secret: marchofthepancakes dot epizy dot com slash index dot php (sorry about this formatting, but apparently no links in posts…), I just checked the source and it’s purely the text I want… Also, I tried the simple urlfetch again, and now I’m getting the Error Connecting to Remote Server thing.

An error connecting to your server means that the connection was rejected for whatever reason. It could be your server is blocking our request, the server is down, or an error was returned from the server.

Ah I forgot to test this for you. But yes, it is not working with the hosting you are using. In the browser it works fine, however there seems to be a sort of protection/security to block requests from non browsers.

When I try to request your website through cURL it returns a html page with an error:

Thank you! I’ll find a different place to host the script, much appreciated.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.