Skip to content

Instantly share code, notes, and snippets.

@denchev
Last active August 29, 2015 14:07

Revisions

  1. denchev revised this gist Oct 28, 2014. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions request.php
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,21 @@ function request($url, $cache = false) {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    $errno = curl_errno($ch);

    $info = curl_getinfo($ch);

    if($info['http_code'] == 504 || $errno == 28) {
    throw new Exception("Unable to open url: " . $url);
    }

    curl_close($ch);

    if($cache === true) {
    file_put_contents($file, $response);
  2. denchev revised this gist Oct 10, 2014. 1 changed file with 31 additions and 28 deletions.
    59 changes: 31 additions & 28 deletions request.php
    Original file line number Diff line number Diff line change
    @@ -2,35 +2,38 @@

    function request($url, $cache = false) {

    if($cache === true) {
    $name = md5($url);

    if(file_exists('/tmp/' . $name)) {
    $response = file_get_contents('/tmp/'.$name);

    return $response;
    $prefix = 'cache_response_';

    if($cache === true) {
    $folder = dirname(__FILE__ ) . DIRECTORY_SEPARATOR . 'cache';
    $folder = file_exists($folder) && is_writable($folder) ? $folder : '/tmp';
    $file = $folder . '/' . $prefix . md5($url);

    if(file_exists($file)) {
    $response = file_get_contents($file);

    return $response;
    }
    }
    }

    $ch = curl_init();

    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $response = curl_exec($ch);

    if($cache === true) {
    $name = md5($url);
    file_put_contents('/tmp/' . $name, $response);
    }

    return $response;

    $ch = curl_init();

    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $response = curl_exec($ch);

    if($cache === true) {
    file_put_contents($file, $response);
    }

    return $response;
    }

    ?>
  3. denchev revised this gist Oct 9, 2014. 1 changed file with 33 additions and 29 deletions.
    62 changes: 33 additions & 29 deletions request.php
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,36 @@
    function request($url, $cache = false) {

    if($cache === true) {
    $name = md5($url);

    if(file_exists('/tmp/' . $name)) {
    $response = file_get_contents('/tmp/'.$name);

    return $response;
    }
    }
    <?php

    $ch = curl_init();

    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $response = curl_exec($ch);
    function request($url, $cache = false) {

    if($cache === true) {
    $name = md5($url);
    file_put_contents('/tmp/' . $name, $response);
    if($cache === true) {
    $name = md5($url);

    if(file_exists('/tmp/' . $name)) {
    $response = file_get_contents('/tmp/'.$name);

    return $response;
    }

    return $response;
    }
    }

    $ch = curl_init();

    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $response = curl_exec($ch);

    if($cache === true) {
    $name = md5($url);
    file_put_contents('/tmp/' . $name, $response);
    }

    return $response;
    }

    ?>
  4. denchev created this gist Oct 9, 2014.
    32 changes: 32 additions & 0 deletions request.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    function request($url, $cache = false) {

    if($cache === true) {
    $name = md5($url);

    if(file_exists('/tmp/' . $name)) {
    $response = file_get_contents('/tmp/'.$name);

    return $response;
    }
    }

    $ch = curl_init();

    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $response = curl_exec($ch);

    if($cache === true) {
    $name = md5($url);
    file_put_contents('/tmp/' . $name, $response);
    }

    return $response;
    }