I have used PHP in order to write a Telegram bot that uses webhook which means that the PHP file is called for each update. I have used curl in order to post JSON to https://api.telegram.org. When I send a message in my PHP file, it responds in around 1 second. I think because it does not reuse the curl connection. the connection closes after the PHP file finishes.
I have also written a Java application that calls getUpdate method every 5 seconds. It responds in around 500 milliseconds because it reuses the connection.
How can I speed up my PHP bot just like my Java application? Is there any way to reuse the connection even when the PHP file is finished and another PHP file is run?
Here is my PHP code:
function Post($method, $data)
{
$url = "https://api.telegram.org/bot".$authenticationToken."/".$method;
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_ENCODING, '');
$resultJSON = curl_exec($curl);
curl_close($curl);
$result = json_decode($resultJSON,true);
if(!$result["ok"])
LogF($resultJSON);
return $result;
}
P.S: the ping time of the URL is around 200 milliseconds.
Related
so I have a webservice running on a Windows machine with a static IP adress and an open port. The webservice provides some json data. When accessing the IP adress and port from the browser the json is being displayed just fine.
[Sample json data from browser:
What I want to do now is fetch the data from this machines' service with php on a website.
I have the following php code on the website:
<?php
$url = 'http://ip-of-the-machine:port/url?params';
$result = file_get_contents($url);
$json = json_decode($result);
var_dump(json_decode($result, true))
?>
When I visit the site the code is on, the page is tuck loading infinetly.
When passing 'https://jsonplaceholder.typicode.com/posts/1' as the url paramter, the sample json data from this website is being displayed.
In the php.ini file allow_url_fopen is set to "true".
So I assume that there is an issue with the source of my json data on my machine where the service is running. Do you have experiance with this issue? What is the problem here?
Thank you!
As it turns out, there was an internal problem with the hosting provider for the website where I run the PHP script that calls the URL to fetch the JSON.
They resolved the issue and now both cURL requests and file_get_contents() work just fine. Thank you all!
I would suggest running this with curl rather then file_get_contents. That allows for more debugging too.
Example:
<?php
$url = 'http://ip-of-the-machine:port/url?params';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); //Tell cURL that it should only spend 10 seconds trying to connect to the URL in question.
curl_setopt($curl, CURLOPT_TIMEOUT, 30); //A given cURL operation should only take 30 seconds max.
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_FILETIME, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
$response=curl_exec($curl);
$info = curl_getinfo($curl); //retrieve all details from the http request thas was being made
//show that information in the browser
echo '<pre>';
print_r($info);
echo '</pre>';($info);
//show the actual respones from the browser
var_dump($response);
curl_close($curl);
Output in your browser (tested with an JSON example URL from https://jsonplaceholder.typicode.com/) would look like this:
EDIT: The proper thing to do is just to send a response from Node-red as hardillb pointed out below.
My CURL request is working fine and instantly, but I simply need to have the page visit the url and not wait around for a response. I have tried every combination I can think of and my browser still sits waiting for a server response until timeout.
$url = 'http://example.com:1880/get?temperature='.$temperature;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 3. execute and fetch the resulting HTML output
$output = curl_exec($ch);
// 4. free up the curl handle
curl_close($ch);
}
As mentioned in the comments.
The correct solution is to ensure your http-in node is paired with a http-response node in your Node-RED flow
I am trying to create a simple PHP script that calls two different REST APIs on two different domains. Both services are HTTPS and require authentication. When I do a curl from the terminal, I get the response in JSON for both domains and everything works beautifully:
curl --user “myuser:mypassword” https://www.example.com/rest/api/2/projects
Notice that it's a GET, not a POST.
The strange thing is that when I try the exact same curl commands from my PHP script neither of them works.
This what happens:
The first domain returns an empty JSON array with no errors. Just this: []
The second domain returns this error in JSON:
{"errors":[
{
"context":null,
"message":"You are not permitted to access this resource",
"exceptionName":"com.atlassian.stash.exception.AuthorisationException"
}
]}
Here's what's NOT happening:
No SSL certificate errors or warnings
No authentication errors.
Even if put in a bad username or password, both services will act exactly the same way.
To me what's suspicious is that both domains don't authenticate my calls which makes me think there's either a problem with my code or in the php curl library.
Here's my code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $link3);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$encodedAuth = base64_encode($username.":".$password);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authentication : Basic ".$encodedAuth));
curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); //get status code
I know some of it is redundant, but I wanted to try everything and nothing works. Any ideas?
My environment:
OS X Yosemite (10.10.2)
PHP 5.6.6 (I manually upgraded to the latest version as an attempt to make this work)
The current code mixes various approaches and does it in a conflicting way:
the authentication header is named Authorization: and not Authentication:
the CURLOPT_HTTPAUTH, CURLAUTH_ANY tries to negotiate with the server and it shouldn't (neither does the working cURL command line)
Just use:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $link3);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
# for debugging/non-prod
#curl_setopt($curl, CURLOPT_VERBOSE, true);
#curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
Need to consume some data from a web service that requires a username/password for access.
The following returns NULL
$service_url = 'https://example.com/2365139.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
When I hit https://example.com/2365139.json in a browser, it prompts for un/pw and when I enter them it displays the JSON, so the data is there but something I have written above isn't working.
Original code works fine, but because the resource is https it requires the following option;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
The caveat to using this is "This basically causes cURL to blindly accept any server certificate, without doing any verification as to which CA signed it, and whether or not that CA is trusted. If you’re at all concerned about the data you’re passing to or receiving from the server, you’ll want to enable this peer verification properly." - Taken from http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
I am using PHP's build in cURL library to make GET requests to the Meetup API. This is an example of a query I'm running to view every meetup group 25 miles from central park:
https://api.meetup.com/groups.json/?lat=40.75&lon=-73.98999786376953&order=members&page=200&offset=0&key=MY_API_KEY
This query works correctly when passed to the browser, it returns the excepted 200 largest groups.
When I run this in a PHP script I'm using cURL set with these options
curl_setopt($cURL, CURLOPT_URL, $groups_url);
curl_setopt($cURL, CURLOPT_HEADER, 1);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$json_string = curl_exec($cURL);
I am hoping to be able to get the cURL to execute and return a json string that I can parse, but for some reason I do not understand, the result of curl_exec is always NULL, I am not sure why an input that works in the browser will not work in a script, this could just be me being dumb. Thank you for your help in advance.
this is becuase its https [SSL].
so the quick fix is to add this line
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 0);
here example of it all working
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "https://api.meetup.com/groups.json/?lat=40.75&lon=-73.98999786376953&order=members&page=200&offset=0&key=MY_API_KEY");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 0);
$json_string = curl_exec($cURL);
echo $json_string;