Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PHP Script working fine from browser but not working when executed from php

20 views
Skip to first unread message

androi...@gmail.com

unread,
Feb 1, 2017, 11:09:20 AM2/1/17
to
Hi everyone, i'm trying to get a php script executed from a workflow in my crm but it does not execute .
If i test it from my browser it works fine and does what it should.
I'm not sure why this happens.
The content of the php script is the following:


<?php
$telegrambot='276929052:AAEdjgS4w3Jf8zuWl3uLhvs-mycode';
$telegramchatid=174428000;


function sendtelegram($msg)
{
global $telegrambot,$telegramchatid;
$msg= "Messaggio dal CRM.";
$url='https://api.telegram.org/bot'.$telegrambot.'/sendMessage';$data=array('chat_id'=>$telegramchatid,'text'=>$msg);
$options=array('http'=>array('method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencoded\r\n",'content'=>http_build_query($data),),);
$context=stream_context_create($options);
$result=file_get_contents($url,false,$context);
return $result;
}
//obbligatorio questo sotto per richiamare la funzione in modo tale che messaggio venga inviato
sendtelegram ("Ciao");
//Example:
//telegram('This is a test message sent to Telegram bot at'.time());
?>

Can anyone help me?
I tried it on a hosted server but also on my local installation using xampp.
The CRM i'm using is VtigerCrm

Thanks in advance.

Jerry Stuckle

unread,
Feb 1, 2017, 1:10:46 PM2/1/17
to
Well, to start with, what do you mean by "not working"? What error
messages do you get?

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

androi...@gmail.com

unread,
Feb 2, 2017, 4:16:54 AM2/2/17
to
Hi, thanks for replying,

when i say it's not working i mean it doesnt send a message on telegram and no error messages are given.

If i launch the script from the browser the message gets sent on telegram, if i try to call the script from php so that it runs as if i called it manually from my browser, it won't send the telegram message and it does not disply any errors.

That's why i'm clueless!

Thanks.

R.Wieser

unread,
Feb 2, 2017, 5:17:40 AM2/2/17
to
Maroso(?),

> ... and no error messages are given.

That may be true, but have you looked at whats returned by the
"file_get_contents" call ? What status do you get back ?

Also,

> ... it doesnt send a message on telegram

this doesn't mean that the request isn't received. Its possible that it is,
but is rejected.

Also, have you already tried to direct that request to a "website" you
control (maybe just a "catch-all" page running under xampp) ? Do you see
it there ? If not ...

In other words: try to make sure of that everything you presume happens
flawlessly actually does so. You would not be the first guy who gets bitten
in the behind by something known to me as "Murphys Laws". :-)

Regards,
Rudy Wieser



Ben Bacarisse

unread,
Feb 2, 2017, 9:36:15 AM2/2/17
to
androi...@gmail.com writes:

> Il giorno mercoledě 1 febbraio 2017 19:10:46 UTC+1, Jerry Stuckle ha scritto:
>> On 2/1/2017 11:09 AM, androi...@gmail.com wrote:
<snip>
>> > The content of the php script is the following:
>> >
>> > <?php
>> > $telegrambot='276929052:AAEdjgS4w3Jf8zuWl3uLhvs-mycode';
>> > $telegramchatid=174428000;
>> >
>> > function sendtelegram($msg)
>> > {
>> > global $telegrambot,$telegramchatid;
>> > $msg= "Messaggio dal CRM.";
>> > $url='https://api.telegram.org/bot'.$telegrambot.'/sendMessage';$data=array('chat_id'=>$telegramchatid,'text'=>$msg);
>> > $options=array('http'=>array('method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencoded\r\n",'content'=>http_build_query($data),),);
>> > $context=stream_context_create($options);
>> > $result=file_get_contents($url,false,$context);
>> > return $result;
>> > }
>> > //obbligatorio questo sotto per richiamare la funzione in modo
>> > tale che messaggio venga inviato
>> > sendtelegram ("Ciao");
>> > //Example:
>> > //telegram('This is a test message sent to Telegram bot at'.time());
>> > ?>
<snip>

> If i launch the script from the browser

This phrase suggests a misunderstanding. PHP runs on webservers, not in
browsers. Presumably what you mean here is that you request a resource
(the browser doing it's usual thing) and the server executes the script
to generate the document the browser receives. Again, that the usual
process -- PHP code runs on the server in response to client request for
those resources that need PHP processing.

If this is not what you mean by "i launch the script from the browser"
I, for one, need to know more. One way is to describe exactly the
actions you take, step by step.

> the message gets sent on
> telegram, if i try to call the script from php so that it runs as if i
> called it manually from my browser, it won't send the telegram message
> and it does not disply any errors.

Similarly, I not sure what calling he script from PHP means. Can you
say exactly what you do, step by step?

<snip>
--
Ben.

Jerry Stuckle

unread,
Feb 2, 2017, 11:36:14 AM2/2/17
to
> Hi, thanks for replying,
>
> when i say it's not working i mean it doesnt send a message on telegram and no error messages are given.
>
> If i launch the script from the browser the message gets sent on telegram, if i try to call the script from php so that it runs as if i called it manually from my browser, it won't send the telegram message and it does not disply any errors.
>
> That's why i'm clueless!
>
> Thanks.
>

OK, well, to start, on your development machine your php.ini file should
have:

error_reporting = E_ALL |
display_errors = on

Ensure you are updating the correct php.ini file - the browser and cli
may use different ones. Executing a file with just the line

<?php phpinfo(); ?>

will show you which you are currently using. Execute it from the CLI.
And while you're at it, check the other options it shows and compare to
the options in your web server.

Also, what version of PHP are you using - both on your web server and in
your cli?

BTW - I prefer to use cURL for operations like this. It's much more
flexible. But your way should work.
0 new messages