Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, this isn't possible with the built in functions of cURL. However, it would be trivial to implement a wrapper around the native functions to do what you want.</p> <p>For instance, vaguely implementing the Observer pattern:</p> <pre><code>&lt;?php class CurlWrapper { private $ch; private $listeners; public function __construct($url) { $this-&gt;ch = curl_init($url); $this-&gt;setopt(CURLOPT_RETURNTRANSFER, true); } public function setopt($opt, $value) { $this-&gt;notify('setopt', array('option' =&gt; $opt, 'value' =&gt; $value)); curl_setopt($this-&gt;ch, $opt, $value); } public function setopt_array($opts) { $this-&gt;notify('setopt_array', array('options' =&gt; $opts)); curl_setopt_array($this-&gt;ch, $opts); } public function exec() { $this-&gt;notify('beforeExec', array()); $ret = curl_exec($this-&gt;ch); $this-&gt;notify('afterExec', array('result' =&gt; $ret)); return $ret; } public function attachListener($event, $fn) { if (is_callable($fn)) { $this-&gt;listeners[$event][] = $fn; } } private function notify($event, $data) { if (isset($this-&gt;listeners[$event])) { foreach ($this-&gt;listeners[$event] as $listener) { $listener($this, $data); } } } } $c = new CurlWrapper('http://stackoverflow.com'); $c-&gt;setopt(CURLOPT_HTTPGET, true); $c-&gt;attachListener('beforeExec', function($handle, $data) { echo "before exec\n"; }); $result = $c-&gt;exec(); echo strlen($result), "\n"; </code></pre> <p>You can add event listeners (which must be callables) to the object with <code>addListener</code>, and they will automatically be called at the relevant moment.</p> <p>Obviously you would need to do some more work to this to make it fit your requirements, but it isn't a bad start, I think.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload