ZF2 Log Log Queries

I need to catch all the curl requests that the application makes at runtime and show them in a new tab in the ZF toolbar.

I created a picker for the ZF Developer toolbar, but I don’t know how to catch all the curls that the application makes at runtime and register them.

+7
php curl zend-framework2
source share
3 answers

After some research, I do not think that there is a general solution for all curl requests (for example, requests from a third-party library). Please correct me if I am wrong.

As you mentioned, you can write your own connector / service that logs your own requests.

You can also register all outgoing traffic from your application using a tool such as tcpdump to a file that displays the contents of this file in DevToolbar. This is not limited to curious requests, which may be best for you.

+2
source share

You can catch all the curl requests that zf2 creates at runtime and log them using CURLOPT_VERBOSE and write them to a single log file using CURLOPT_WRITEHEADER or curl_getinfo () and read it to show it on the ZF Developer toolbar this way ..

Prepare a curl request with the parameters CURLOPT_VERBOSE and CURLOPT_WRITEHEADER, as shown 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 \Zend\Http\Client\Adapter\Curl(); $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); } 

Explanation:

  • Prepare the log file to add the request data to this log file.
  • Prepare the curl request for the parameters CURLOPT_VERBOSE and CURLOPT_WRITEHEADER.
  • Fulfillment of a request for curling.
  • Get turbulence information using curl_getinfo ().
  • Recording curl request information in a log file

After that, you can read the log file using the zend file or fread () to show it on the developer's toolbar.

OR

In addition, there are alternative third-party solutions that will track your server traffic using netstat or tcpdump or wirehark, as follows.

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 tcpdump page for more details.

+1
source share

If you use Zend_Http_Client, you can go from the CURL-Adapter class and overwrite the write method, where you can register a call before calling the write method of your parent class.

Maybe 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 look like.

+1
source share

All Articles