How does i execute Cron job using browse direct URL - php

My application run on hostgater.com .
i created a Cron job scheduler in my server and set all those parameters.
it working good far
command parameter like /home3/user/public_html/sendemial.php.
it is not workging i try to execute the MVC controller action URL like
command parameter like http://voola.org/user/sendemail
i also tried to using iframe, but it is not workin. i am very worry about to execute direct url in Hostgater server.
any help will be appreciate :)

examoleThanks #Orangepill, you are great. using curl its working charm. below is my code example
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://sadf/profiles/emailalert");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$result=curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
if($result){
echo "execute by curl ok and sending mail";
}else{
echo "not execute curl funation";
}

Read the manual for running from command line:
http://ellislab.com/codeigniter/user-guide/general/cli.html

Related

how to get JSON data from unusual port in php?

i have scout_realtime, and i want to get stats.json from it. in debug it looks like
91.205.168.39 - - [10/Nov/2018:09:54:22 CET] "GET /stats.json HTTP/1.1" 200 3896
http://188.165.3.*:5556/ -> /stats.json
how to get it manially? I tried to do something like this:
$status = file_get_contents('http://188.165.3.*:5556/stats.gson')
but it doesnt work
$url = 'http://188.165.3.*:5556/stats.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
ok i made it myself :D
You have several possibilities to fetch your datas.
Firstly you can use cURL. CURL is really good to send requests. As you showed in your own answer you can send an HTTP request in only few lines. I won't show a CURL example because it's already done in your answer.
Please note that you can use libraries like Guzzle to send requests, it's easier.
Then, I'm not sure about what doesn't work in your code, but it is possible to fetch datas on a given port using file_get_contents. Here is the little example tested with PHP 7.2.10.
server.php
<?php
echo 'Hi from server';
client.php
<?php
echo file_get_contents('http://localhost:8080/server.php');
server.php acts as the server you are trying to hit. It's supposed to return a simple string.
client.php is the script that's trying to fetch the datas.
Then if you run php -S localhost:8080 -t . in the directory where server.php is, you can execute the client.
Please note that you can configure the behaviour of file_get_contents by giving it a context parameter. See the stream_context_create documentation to learn more about it.
This answer aims to show that it is possible to fetch datas using file_get_contents, but if you need some specific parameters to fetch your datas like configuring a proxy, a port or something more complexe that just read the file, I suggest to use an HTTP-dedicated function or library, like cURL or Guzzle.

Asynchronus call in PHP Web page

I am new to PHP. I have a block of code on my webpage which I want to execute asynchronously. This block has following :
1. A shell_exec command.
2. A ftp_get_content.
3. Two image resize.
4. One call to mysql for insert.
Is there way make this block async, So that the rest of the page loads quickly.
Please ask if any more details required.
One possible solution is to use curl to do a pseudo async call. You can put the async part of your code in a separate php file and call it via curl. For example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'YOUR_URL_WITH_ASYNC_CODE');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
You could put the 4 tasks into a queue, maybe something like Beanstalkd, then have a background worker process this queue.

Run url in background in php

In my application i have to send the sms to user while registration. But while inserting record in database i want to hit this url in browser.
Can any one suggest how to run this url at backgound
http://www.myurl.com/smpp/sendsms?username=XX&password=XX&to=XX&from=XX&text=Test
Well this depends on what you mean with background I'm asuming however that you mean that the user won't be redirected to that page.
If I were you I'd go with cURL if you have it installed, since the only thing you seem to want to do is make an ordinary request, and maybe, read the response. The code below is untested but should give you a hint.
$req = curl_init();
curl_setopt($req, CURLOPT_URL,"theaddress_and_params");
curl_exec($req);
public function get_url($url)
{
$cmd = "curl --max-time 60 ";
$cmd .= "'" . $url . "'";
$cmd .= " > /dev/null 2>&1 &";
exec($cmd, $output, $exit);
return $exit == 0;
}
it will call curl via cli. it will run in background.
If you did that you would be exposing usernames and passwords in the URL (or headers). Have the user login in advance and use a session variable.
Don't send this from the client side since every user would easily be able to "fake" the data by just loading your URL with some (potentially malicious) parameters. "username" and "password" are not protected at all and I'm sure your service would be down very quickly.
Instead, you could easily do this in the background (server-side) with PHPs curl functions:
http://www.php.net/manual/en/curl.examples-basic.php
$url = 'http://yoursmsgateway.com/WebSMS/SMSAPI.jsp?username='.$smsuser.'&password='.$smspwd.'&sendername='.$smssender.'&mobileno='.$number.'&message='.urlencode($message);
echo $url;
$mystring = get_data($url);
//echo "hi!";
echo $mystring;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
You can try this code, But you have to install cUrl DLL file. Process to install CURL is given below:--
1.)open php.ini file
2.)Find this dll file---> ;extension=php_curl.dll
3.)Remove ; (semicolon)
4.)such as---> extension=php_curl.dll
5.)Save it (Ctrl+s)
you could fork a child-process with the pcntl php extension.
(library that implements this: https://github.com/kriswallsmith/spork)
Make an AJAX call as the user hits the submit button. This would cause the script at that URL to run in the background while your current PHP inserts the record in the database.
Ajax? Load that url inside a div with ajax while you save the record calling another php file with ajax.
I think that there is no such concept as multithreading (which in essence is what you are asking for), as everything in a PHP code runs incrementally, but you can get to a solution. See this and this questions and their answers.
The reason that there is no multithreading in PHP is because everything is processed in the server, and you, as a client, already receive a finished response, so "running on background" in PHP is the same as "running sequentially".

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);

cURL hangs on request, waits for timeout to proceed

I'm encountering a problem to which I can't find a solution anywhere. Even worse, none else seems to have this problem so I'm probably doing something very stupid.
Some background info: I'm trying to make a proxy-like page that forwards an AJAX request to a different server. This to circumvent the same-domain-policy. All I want this code to do is take the POST variables, forward them to a different page, and then return the results. It's been working but for 1 thing: every time it waits for the timeout to continue. I've put it to 1 second now, so it's doing ok for now, but I'd rather have a fast response and proper timeout.
Here's my code:
// create a new cURL resource
$call = curl_init();
// set URL and other appropriate options
curl_setopt($call, CURLOPT_URL, $url);
curl_setopt($call, CURLOPT_POST, true);
curl_setopt($call, CURLOPT_POSTFIELDS, $params);
curl_setopt($call, CURLOPT_HEADER, false);
curl_setopt($call, CURLOPT_RETURNTRANSFER, true);
curl_setopt($call, CURLOPT_CONNECTTIMEOUT, 1);
// grab URL and pass it to the browser
$response = curl_exec($call);
// close cURL resource, and free up system resources
curl_close($call);
echo $response;
I've tried sending a "Connection: close" header with it, and several ways to make the target code specify that it's done running (setting Content-length, flushing, die(), etc.). At this point I really don't know what's going on, what surprises me most is that I can't find anyone with a similar problem.
Who can help me?
This would make sense if the server weren't actually completing the request. This would be expected in a page streaming or service streaming scenario. Are you sure that the server is actually returning a full and complete HTTP response to each request?
Sounds like it's trying to connect, timing out, and the retry is working.
This fixed it for me:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
I can connect on the commandline via ipv6, so I don't know why this helps.

Categories