Beginner at PHP - having issues with cURL - php

I am having some issues getting cURL to work in PHP. I'm a complete beginner (as of a few days ago) with PHP.
Basically, I'm trying to grab the output from my Arduino (which is outputting the temperature every 10 seconds) and bring it across to my web-page. The plan is to then store the data in a database of some kind so I can analyse the history/plot graphs etc over time.
Right now I just need to bring the data across though.
The Arduino is spitting out the temp, nothing else, and my router is making that available on the web here - http://2.216.137.236/
My code is as follows:
<?php
$Url = "http://2.216.137.236/";
echo "Hello, is this on?" . "<br>";
// is cURL installed?
if (!function_exists('curl_init')){
echo "cURL not installed!";
die('Sorry cURL is not installed!');
}
echo "cURL is installed!" . "<br>";
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 1);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
print_r ($output);
?>
But right now all I am getting at my page is
"Hello, is this on?
cURL is installed!"
with no sign of the temperature anywhere (see here - http://wetdreams.org.uk/ChrisProject/curl_test.php)
(excuse the URL, kayaking website of mine)
I really hope there isn't some n00by PHP thing that is killing me.
Anyway, hope you can help me out.
Edit: My poor little router has crashed trying to serve all your requests, in hindsight probably shouldn't have put the IP up. Will reset it and try again, links might not work afterwards.

Eventually this article may help you.
The simples way would be to use file_get_contents("url") but you can of course use curl if you want.

You can use php's file_get_contents function if you only need to get what's on the page : http://php.net/manual/fr/function.file-get-contents.php
It's easier for your needs i guess.
Example :
<?php
$temperature = file_get_contents("http://2.216.137.236/");
// Process your data here
?>

I tried your code, and added this in:
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
The HTTP code returns 0, and the error is: Curl error: Empty reply from server.
I can't say what's causing this, but it should help to debug further. (I can see the webpage's output for the temp in my browser - so I really can't understand why the page is not being served to Curl)

Related

Curl error: operation aborted by callback

So I have obviously googled the error - but PHP (PHP 7.4.4 (cli)) curl gives me the error:
Curl error: operation aborted by callback with the following code:
private function curl_post($url,$post,$file = null,$file_type = 'audio/wav'){
$ch = curl_init($url);
if (!empty($file)){
$post['SoundFile'] = new CURLFile(UPLOAD_PATH.$file,$file_type,$file);
}
// Assign POST data
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)) echo 'Curl error: '.curl_error($ch);
curl_close($ch);
print'<pre>Curl (rec): '."\n";print_r($result);print'</pre>';
}
I control both (Ubuntu) servers and have rebooted them both. I am posting a fairly large amount of data but in the Google searching this didn't seem to be what is triggering the curl_error. Does anyone know what is causing it? It was working perfectly fine and then it stopped.
Additionally putting file_put_contents(time().'.txt','log'); as a break in my receiving server does log the response. So its clearly landing in the right area.
Additionally what I will say is that the 2 servers talk a number of times to each other through curl (so one curls to one then back a bit). Furthermore - error 42 is the CURL response but https://curl.haxx.se/libcurl/c/libcurl-errors.html doesn't seem to provide much help. I've tried tracing the various calls to each other and can't see why it is breaking - it errors/breaks before the post/calls even occur.
So I found the answer and I hope this helps anyone else in this situation. The reason being was because the file was missing on the server for the CURLFile (having previously been there). My code now reads:
if (!empty($file) && is_file(UPLOAD_PATH.$file)){
$post['SoundFile'] = new CURLFile(UPLOAD_PATH.$file,$file_type,$file);
}
And this no longer generates the error. The key was that it errored even before submitting the post but the error itself wasn't that helpful until I broke it down in a separate test script and added the file element back in.

file_get_contetnts and soap client don't work with long query

Sorry if this is duplicate,I would think it would be but couldn't find anything
I have an application using soap client web-service to connect to other server but recently I found out the requests with long text do not send to web-service.
I thought it related to some PHP configs and set post_max_size to 200M and set max_input_vars to 10000 but the problem did not solve ...
today I found out file_get_contents don't work as well as soap client in my server and I am sure one of php configuration made this problem.
here is the code that dont work and when I run this just wait for a while and display nothing ( white page)
$url="http://somedomain.com/webservice.php?Username=user&Password=pass&Det=13218%5E04134414401%5E7015373%5E3%5E51%5E14%252F2141-9305-310209-424926%25D8%25A2%25D9%2585%25D9%2588%25D8%25B2%25D8%25B4%2B%25D9%2583%25D8%25A7%25D8%25B1%25D8%25A8%25D8%25B1%25D8%25AF%25D9%258A%2BHTML%2B5.0%2B%2526%2BCSS%2B3.0%2B%25D9%2587%25D9%2585%25D8%25B1%25D8%25A7%25D9%2587%2B%25D8%25A8%25D8%25A7%2BResponsive%2BWeb%2BDesign%25D9%2585%25D8%25B1%25D8%25AC%25D8%25B9%2B%25D8%25A2%25D9%2585%25D9%2588%25D8%25B2%25D8%25B4%25D9%258A%2B%25D8%25A8%25D8%25B1%25D9%2586%25D8%25A7%25D9%2585%25D9%2587%25E2%2580%258C%25D9%2586%25D9%2588%25D9%258A%25D8%25B3%25D9%258A%2BASP.NET%2B4.5%2B%25D8%25AF%25D8%25B1%2BC%2523%2B%2528%25D8%25A7%25D8%25B2%2B%25D9%2585%25D8%25A8%25D8%25AA%25D8%25AF%25D9%258A%2B%25D8%25AA%25D8%25A7%2B%25D9%25BE%25D9%258A%25D8%25B4%25D8%25B1%25D9%2581%25D8%25AA%25D9%2587%2529%25D9%2585%25D8%25B1%25D8%25AC%25D8%25B9%2B%25D9%2583%25D8%25A7%25D9%2585%25D9%2584%2BASP.Net%2BMVC%2B4%5E2825%5E819700%5E1%5E0%5E%25D8%25A7%25D9%2585%25DB%258C%25D8%25B1%25D8%25B1%25D8%25B6%25D8%25A7%2B%25D8%25A7%25D8%25B3%25D8%25AF%25D9%25BE%25D9%2588%25D8%25B1%5E%25D8%25AA%25D8%25A8%25D8%25B1%25DB%258C%25D8%25B2%2B-%2B%25DA%2586%25D9%2587%25D8%25A7%25D8%25B1%25D8%25B1%25D8%25A7%25D9%2587%2B%25D9%2584%25D8%25A7%25D9%2584%25D9%2587%2B%25D9%2585%25D8%25AC%25D8%25AA%25D9%2585%25D8%25B9%2B%25D8%25AA%25D8%25AC%25D8%25A7%25D8%25B1%25DB%258C%2B%25D9%2584%25D8%25A7%25D9%2584%25D9%2587%2B%25D8%25B7%25D8%25A8%25D9%2582%25D9%2587%2B%25D8%25A7%25D9%2588%25D9%2584%2B%25D9%2588%25D8%25A7%25D8%25AD%25D8%25AF%2B5%5E5178735466%5Einfo%2540pendarepars.com%5E154000%5E12320&Ordertip=1";
echo file_get_contents($url);
You can avoid this issue by sending the request as a post not Get
You should check what the server is responding with using cUrl. This snippet should help you on your way.
// Prep cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
// Output info
echo "<pre>";
if(curl_error($ch))
{
echo "Error: ".curl_error($ch)."\n";
}
else
{
print_r(curl_getinfo($ch));
echo htmlentities($return);
}
Look for http_code, if it's 414 then the server can't handle the length of the URI.

How to determine why cURL transfer is failing?

I've been using cURL to get the output of an external page and it's worked great for months, but suddenly it stopped working. My code is like this:
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
The URL is valid, I checked that it still works and it does, and through debugging I found that the $output variable's value is false, which according to the PHP manual is what curl_exec() returns on failure.
So, after working for a long time, and without any changes to my code (that I know of), the cURL transfer is suddenly not working.
How can I debug why it's not working?
I would start with curl_error()
You can use the curl_error() function to see the error returned by curl.

CURL can't connect? Just hangs and doesn't respond. PHP has it enabled

I have a simple PHP function on a friend server which I've checked and has PHP CURL enabled.
The function is:
function sw_fetch_code($apikey='',$email=''){
$url = "http://www.domain.com/xxx/api.php?getcode=1&apikey=".$apikey."&email=".$email."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
if(!empty($obj)){
if($obj->status == 200){
return $obj->code;
}else{
return $obj->status;
}
}
}
As you can see this is very simple and I've tested it and works on localhost and internal to my own server. The url returns as expected. However it just doesn't give any response when this function is called on my friends server.
Any ideas what could cause this?
First :
check from the "friend" server if the URL works, as you donot have POST params, you can check with the exact query and get expected results. See if you can get the results on the browser on the friend server. If you don't have a GUI try wget on the command line. See if you get results. If you do go to next step, if you don't cURL isn't the problem. "friend server" isn't able to see your domain. Could be network issue / hosts etc .. (more on that if its the case)
Second:
If you see results on step 1. Try this and see if you get anything:
$handle = fopen($url, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle,1024);
}
If you get response to this, then there is something wrong with cURL.
Does the curl_exec() call fail immediately, or does it hang for 30 seconds or so until it times out? If the latter, you may want to check for a firewall issue.
What does curl_getinfo($ch) tell you?
I think you should begin with standart checks:
If php is compiled with php_curl extension (or the extension is available as shared object). You can check it by putting a
<?php
if (!extension_loaded('curl'))
{
if (!dl('curl.so')) {
die('Cannot load php_curl extension');
}
}
?>
If the extension is loaded there might be a problem with dns/firewall on the friend's server. There also might be a requirement to use proxy server.

cURL PHP: Connecting to Invoicing System

I am attempting to use cURL to connect to a page like this: https://clients.mindbodyonline.com/asp/home.asp?studioid=851 with the following code;
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'https://clients.mindbodyonline.com/asp/home.asp?studioid=851');
//curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl_handle,CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($curl_handle,CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
//curl_setopt($curl_handle,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($curl_handle,CURLOPT_HEADER, 1);
//curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER, 1);
//curl_setopt ($curl_handle,CURLOPT_POST, 1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer))
{
print "Sorry, The booking system appears to be unavailable at this time.<p>";
}
else
{
print $buffer;
}
?>
I've fiddled the settings and the only three responses I get are;
Nothing is loaded and the error message is called
A redirect to /asp/home... locally
Returns '1' and that's all
Thanks for your time!
1. Nothing is loaded: Your code works fine, but they're ignoring you. It's possibly some anti-hammering functionality on their end. You can always change your code to sleep for a while and retry a few times.
2. A redirect to /asp/home... locally: Your code is working, but returns javascript back from their page which is executed by your browser and redirects you to a page that doesn't exist. To see this code without it running do:
print htmlspecialchars($buffer);
3. Returns '1' and that's all: That happens if you don't use
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
Uncomment it.
Unfortunately even then you wont be able to read this particular site as is, because it's drawn by javascript, which cURL can't run.
Perhaps the cookies you are using are invalid (ie, the session has expired).
If you load that page without cookies, you get a POST form. Perhaps you should send that POST data first, get the cookies and then use those cookies for the rest of the session.
Also, you need to set CURLOPT_RETURNTRANSFER to 1 in order to use $buffer like that. If not, cURL will output the page it gets, which is probably explains #2.

Categories