/** * @internal * * @param array $request * * @return array */ public function _invokeAsArray(array $request) { $factory = $this->factory; // Ensure headers are by reference. They're updated elsewhere. $result = $factory($request, curl_init()); $h = $result[0]; $hd =& $result[1]; $body = $result[2]; Core::doSleep($request); try { // override the default body stream with the request response $safecurl = new SafeCurl($h); $body = $safecurl->execute(Core::url($request)); } catch (Exception $e) { // URL wasn't safe, return empty content $body = ''; $safeCurlError = $e->getMessage(); } $response = ['transfer_stats' => curl_getinfo($h)]; $response['curl']['error'] = curl_error($h); $response['curl']['errno'] = curl_errno($h); $response['transfer_stats'] = array_merge($response['transfer_stats'], $response['curl']); curl_close($h); // override default error message in case of SafeCurl error if (isset($safeCurlError)) { $response['err_message'] = $safeCurlError; } return CurlFactory::createResponse([$this, '_invokeAsArray'], $request, $response, $hd, Stream::factory($body)); }
<?php /* * redirects.php * * Using SafeCurl and following redirects with a limit */ require '../vendor/autoload.php'; use fin1te\SafeCurl\SafeCurl; use fin1te\SafeCurl\Options; try { $curlHandle = curl_init(); $options = new Options(); //Follow redirects, but limit to 10 $options->enableFollowLocation()->setFollowLocationLimit(10); $result = SafeCurl::execute('http://fin1te.net', $curlHandle); } catch (Exception $e) { //Handle exception }
<?php /* * options.php * * Using SafeCurl with custom options */ require '../vendor/autoload.php'; use fin1te\SafeCurl\SafeCurl; use fin1te\SafeCurl\Options; try { $curlHandle = curl_init(); $options = new Options(); //Completely clear the whitelist $options->setList('whitelist', []); //Completely clear the blacklist $options->setList('blacklist', []); //Set the domain whitelist only $options->setList('whitelist', ['google.com', 'youtube.com'], 'domain'); $result = SafeCurl::execute('http://www.youtube.com', $curlHandle); } catch (Exception $e) { //Handle exception }