I want to write a script to call one of my Restful APIs every one hour.
I use Laravel framework.
Here are the codes:
TestController.php
class TestController extends BaseController
{
public function __construct(ItemCriteria $itemCriteria)
{
parent::__construct($itemCriteria);
}
public function sayHi()
{
echo 'hi';
}
public function getHi()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:8888/api/v1/sayhi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
}
}
routes.php
:
Route::group(array('prefix' => 'api/v1'), function () {
Route::get('sayhi', 'Api\TestController@sayHi');
Route::get('gethi', 'Api\TestController@getHi');
}
I call api/v1/gethi
, but the response never comes back. Browse just shows loading.
What I have tried:
Searched on Google, many people say this is due to the session being locked. Therefore, I add the code to close session before curl_exec
:
public function getHi()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:8888/api/v1/sayhi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
session_write_close();
$output = curl_exec($ch);
curl_close($ch);
echo $output;
}
Sadly, this does not work. I check phpinfo()
, sessions should not start automatically as per my settings: session.auto_start Off
.
I also did a small experiment: made an API api/vi/sleep
, which forces the thread to sleep for 10 seconds. During this time I call api/v1/sayhi
, I found it won't respond until first call is finished.
public function sleep()
{
sleep(10);
}
I thought this might be Apache thread configuration problem, but all configurations seem fine:
<IfModule mpm_winnt_module>
ThreadsPerChild 150
MaxConnectionsPerChild 0
</IfModule>
My environment:
I am stuck with this for a few weeks. I don't know that else I can do...