Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF: http request from one controller to onther hangs

I've this problem, I want to perform HTTP request from one of my controllers to URL that is linked with another controller. They are completely independent of each other.

When my controller contains following code my applications hangs until I get Fatal error: maximum execution time of 30 seconds exceeded ...

    $opts = array(
        'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: DEFAULTID=rookgqj7bdi4os6f4pt5vqkk74\r\n"
        )
    );
    $context = stream_context_create($opts);

    $contents = file_get_contents('http://10.10.3.6/__env/Module/post-types-list/json-export', false, $context);
    print "xxx=". $contents;

AFAIK there is no loop here. If i do it from outside ZF it works ok.

like image 293
Sebastian Dusza Avatar asked Nov 08 '12 15:11

Sebastian Dusza


2 Answers

You were not clear about this, but I guess you are using a session in both controllers.

Sessions use a lock to prevent other scripts running at the same time from destroying the data. When using a session, the call to session_start() establishes the lock on the session file, and then reads the data. At the end of the script the changed session data is written back, and the lock is released.

You cannot access the webserver re-using the same session that is currently running with your current request, because you will end in a deadlock that way.

There is however a way out: session_write_close() will write the session data and release the lock as well, but after this your session data cannot be changed, unless you restart the session again with a call to session_start().

Be careful though: It will work, but if you complicate the scenario, it will get you into another deadlock if you do not pay close attention on which session lock is active. It will be way better to solve this problem by executing the other code without creating a new http request, because the code will then execute in the context of the current request, and session locks will not matter.

like image 61
Sven Avatar answered Oct 27 '22 00:10

Sven


I don't have experience with ZF. but to me that's an headers missmatch. your URL

http://10.10.3.6/__env/Module/post-types-list/json-export 

is a controller/action request? and the response is in json format?

if so you could use curl:

$ch = curl_init();

// set URL and other appropriate options
$options = array(
    CURLOPT_URL => 'http://www.example.com/',
    CURLOPT_HEADER => 'Accept: application/json', // this is the tricky bit
    ...
);

curl_setopt_array($ch, $options);

$data = json_decode(curl_exec($ch));

//you then can even debug what's wrong with.
$curl_info = (curl_getinfo($ch));
var_dump($curl_info);

// dont forget to close curl
curl_close($ch);
like image 43
njasm Avatar answered Oct 26 '22 22:10

njasm