ZF2 log curl requests

I need to catch all curl request that application makes during the execution and show them in a new tab in ZF Developer Tools toolbar.

I created a collector for the ZF Developer Toolbar, but dont now how to catch all curl requests that application makes during the execution and log them.


After some research I don't think that there is a general solution for all curl requests (fe requests from some third party library). Please corret me if I'm wrong.

As you mentioned you could write your own connector / service which logs your own requests.

You could also log all outgoing traffic form your application with a tool like tcpdump into a file an show the content of this file in the DevToolbar. This would not be limited to curl requests only, which maybe is better for you.


You can catch all curl requests that zf2 application makes during the execution and log them by using CURLOPT_VERBOSE and writing it into one log file by using CURLOPT_WRITEHEADER or curl_getinfo() and read it to show it on ZF Developer Toolbar like this way..

Prepare curl request having CURLOPT_VERBOSE and CURLOPT_WRITEHEADER options like below..

function curl_request($url, $log_file_path) {

    //1. Prepare log file to append request details in to this log file
    $logfile_fp = fopen($log_file_path, "a+"); 

    //2. Prepare curl request to having CURLOPT_VERBOSE and CURLOPT_WRITEHEADER parameters in it
    $request = new Request();
    $request->setUri($url);
    $request->setMethod('POST');
    $client = new Client();
    $adapter = new ZendHttpClientAdapterCurl();
    $client->setAdapter($adapter);
    $adapter->setOptions(array(
        'curloptions' => array(
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_VERBOSE => 1, 
        CURLOPT_WRITEHEADER => $logfile_fp,
        // Your curl request options here...
        // Your curl request options here...
        // Your curl request options here...
        // Your curl request options here...
        // Your curl request options here...
        )
    ));
    //3. Execute curl request
    $response = $client->dispatch($request);

    //4. Get curl request info
    $handle = $client->getAdapter()->getHandle();
    $request_info = curl_getinfo($handle);

    //5. Write curl request info into log file
    @fwrite($logfile_fp, implode(",", $request_info);
    @fclose($logfile_fp);
}

Explaination :

  • Prepare log file to append request details in to this log file.
  • Prepare curl request to having CURLOPT_VERBOSE and CURLOPT_WRITEHEADER parameters in it.
  • Execute curl request.
  • Get curl request info using curl_getinfo().
  • Write curl request info into log file
  • After this you can read log file using zend file reader or fread() to show it on developer toolbar.

    OR

    Apart from this there are alternative third party workarounds which will track your server traffic by using netstat or tcpdump or wireshark like following way..

    You can use netstat. For example:

    $ netstat -nputw
    (Not all processes could be identified, non-owned process info
     will not be shown, you would have to be root to see it all.)
    Active Internet connections (w/o servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 192.168.2.48:60614      151.101.65.69:80        ESTABLISHED 2527/chrome     
    tcp        0      0 192.168.2.48:58317      198.252.206.25:443      ESTABLISHED 2527/chrome     
    

    Read the netstat's man page for more details.

    OR

    You can use tcpdump tool on the server outside of your apache to track all network traffic, For example:

    $ tcpdump -vv -s0 tcp port 80 -w /tmp/apache_outgoing.pcap
    

    Read the tcpdump's man page for more details.


    If you are using the Zend_Http_Client you can extend from the CURL-Adapter class and overwrite the write-method where you can log the call before calling the write-method of its parent class.

    Could be something like this (draft):

    <?php
    $adapter = new MY_Zend_Http_Client_Adapter_Curl();
    $client = new Zend_Http_Client();
    $client->setAdapter($adapter);
    $client->request();
    
    class MY_Zend_Http_Client_Adapter_Curl {
        public function write()
        {
            // do the logging
            parent.write();
        }
    }
    

    In Zend 2 it should be similar.

    链接地址: http://www.djcxy.com/p/93892.html

    上一篇: 一个单独的循环减慢了一个独立的早期循环?

    下一篇: ZF2记录卷曲请求