Just call a https URL with PHP (without waiting for response) - php

I already googled and searched for an answer, and found also one, but I still get an error...
I just want to start a cronjob by calling a certain URL but I get this failure message:
PHP Warning: fopen(https://my-url.de): failed to open stream: HTTP request failed! in /var/www/vhosts/httpdocs/cronjob/import.php on line 41
I tried this one:
ignore_user_abort(true);
set_time_limit(0);
But it is not working... The url triggers a cronjob but this one needs to long to response...
So what would you suggest to call a cronjob in PHP?
I must call the cronjob by URL and I do not need to get a response!
Greetings and Thank You!

you can use curl instead of fopen as follows
$url = 'https://my-url.de';
$ch = curl_init($url);
// this will prevent printing out the contents , unless you are make echo $content
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

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.

Simple html dom not working for a table[class]

I want to use the contents of a website and include them in mine with "Simple Html DOM", unfortunately the code which normally works for another websites, fails in this specific case:
<?php
// Note you must download the php files from the link above
// and link to them on this line below.
include_once('simple_html_dom.php');
$html = file_get_html('http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
$elem = $html->find('table[class=Descrizione]', 0);
echo $elem;
?>
Warning:
file_get_contents(http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.1 500 Internal Server Error in
public_html/elin-custom-code/simple_html_dom.php on
line 75
Fatal error: Call to a member function find() on a non-object in
public_html/elin-custom-code/sklad-1.php on line 9
As a link for the reference:-
file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.
You can use CURL or a PHP client library like Guzzle.
To be make sure i just use CURL to check what is the exact problem with the help of below code:-
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo "<pre/>";print_r($query);
?>
Output:- http://prntscr.com/a91nl5
So it seems that for security reason these method calls(CURL and file_get_contents) are blocked on that site.
You can try this link answers also, may be you will get some useful output:-
PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
Good Luck.

File get content failed to open stream - Json response

For now i'm testing using file_get_content(url), but it's returning an error.
The request and response are in the same server:
warning: file_get_contents(https://192.168.1.15/adverts/locations): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in xxxxx
The Web-service returns a json object, so the function file_get_contents will receive a json.
I want to use cURL approach, but it doesn't work too.
So i thought to start ,fix this error first and get contents, and after try to debug why cURL is not working, since i'm getting content with this function
{"status":"success","alerts":[],"content":{"types":[{"id":"P","description":"Permanent"},{"id":"C","description":"Contract"}]}}
Try this with your webservice url and tell us if it throws some kind of Exception :
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "192.168.1.15/adverts/locations");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Are you sure you can use HTTPS with your server ? Have you the right settings and certificates ?

file_get_contents failed to open stream with a valid url

I'm trying to use this code in a php file.
I want get the source code from this url and parse the content.
<?php
$fuente = file_get_contents('http://www.akiracomics.com');
echo $fuente;
?>
The problem is, after execute the code I received this error
Warning: file_get_contents(http://www.akiracomics.com) [function.file-get-contents]: failed to open stream: Connection timed out in XXXXXX/test.php on line 2
I tried from the same server to other url and works perfect.
Any idea?
Thanx
It's working here, probably you're making too many requests (what are you trying to do, by the way?) and they are blocking incoming requests from your server.
I finally found the solution. I need to use a curl_exec instead file_get_contents.
With this code, everything works fine.
$curlIMG = curl_init();
curl_setopt($curlIMG, CURLOPT_URL, "http://www.google.es");
curl_setopt($curlIMG, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlIMG, CURLOPT_HEADER, false);
$imgBinary = curl_exec($curlIMG);
curl_close($curlIMG);

This curl command doesn't work for some reason

I have a URL that I need to "call" from a PHP script. The URL makes a phone call using Tropo's API, so it's easy to verify if it was called.
My PHP looks like this:
<?php
$oid=$_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
echo $notify_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $notify_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
mail('me#gmail.com', 'cURL', "Did a cURL");
?>
This prints out the $notify_url variable, and when I take the printed value and enter it into a browser window, I get the desired result i.e. the phone call to my phone.
I have tried this on two web hosts that claim they support CURL, one is paid (crazydomains.com.ayu - just got off the phone to support) and the other is 000webhost.com
Am I doing something wrong? This one is kind of confusing, since it should be so simple.
EDIT: I receive the mail as expected.
EDIT 2: If you have any ideas about how I can debug this, I would appreciate it.
EDIT 3: As Juhana suggested I added echo curl_error(); after curl_exec and I got this error ...
Warning: Wrong parameter count for curl_error() in /home/a5352876/public_html/curl.php on line 15
EDIT 4: changed the echo curl_error() to echo curl_error($ch) and got the message couldn't connect to host so that seems to be the problem.
Now the question is, why can't it connect to a host that is easily accessible through a browser, is there anywhere I can look for that?
If your host has HTTP wrappers enabled, and the allow_url_fopen config option is enabled, then you don't need cURL, especially for such a simple request.
It looks like all you need to do is open a URL, which you can easily do with fopen():
$oid = $_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
$fh = fopen( $notify_url, 'r');
fclose( $fh);

Categories