Skip to content

Instantly share code, notes, and snippets.

@ericstone57
Created May 27, 2014 15:17
Show Gist options
  • Select an option

  • Save ericstone57/4b4f473a0fb0d703dd30 to your computer and use it in GitHub Desktop.

Select an option

Save ericstone57/4b4f473a0fb0d703dd30 to your computer and use it in GitHub Desktop.

Revisions

  1. ericstone57 created this gist May 27, 2014.
    55 changes: 55 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    /**
    * Send a POST requst using cURL
    * @param string $url to request
    * @param array $post values to send
    * @param array $options for cURL
    * @return string
    */
    function curl_post($url, array $post = NULL, array $options = array())
    {
    $defaults = array(
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    CURLOPT_URL => $url,
    CURLOPT_FRESH_CONNECT => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FORBID_REUSE => 1,
    CURLOPT_TIMEOUT => 4,
    CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
    trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
    }

    /**
    * Send a GET requst using cURL
    * @param string $url to request
    * @param array $get values to send
    * @param array $options for cURL
    * @return string
    */
    function curl_get($url, array $get = NULL, array $options = array())
    {
    $defaults = array(
    CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get),
    CURLOPT_HEADER => 0,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT => 4
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
    trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
    }