Giter Club home page Giter Club logo

curl's Introduction

Curl Wrapper for Yii framework PHP v1.1

Update

  • This can now be used for non-Yii applications as well. (still works as a component if you're using Yii)
  • Lazy initialization of curl.
    • getHeader method

Requirements

  • PHP 5.3+
  • Yii 1.1.7 (should work on older versions too)
  • Curl and php-curl installed

Setup instructions

  • Place Curl.php or git clone into protected/extensions/curl folder of your project
  • in main.php, or console.php add the following to 'components':
	'curl' => array(
		'class' => 'ext.curl.Curl',
		'options' => array(/* additional curl options */),
	),

Usage

  • to GET a page with default params
	$output = Yii::app()->curl->get($url, $params);
	// output will contain the result of the query
	// $params - query that'll be appended to the url
  • to POST data to a page
	$output = Yii::app()->curl->post($url, $data);
	// $data - data that will be POSTed
  • to PUT data
	$output = Yii::app()->curl->put($url, $data, $params);
	// $data - data that will be sent in the body of the PUT
  • to PATCH data
	$output = Yii::app()->curl->patch($url, $data);
	// $data - data that will be PATCHed
  • to DELETE
	$output = Yii::app()->curl->delete($url, $params);
	// $params - query that'll be appended to the url
  • to set options before GET or POST
	$output = Yii::app()->curl->setOption($name, $value)->get($url, $params);
	// $name & $value - CURL options
	$output = Yii::app()->curl->setOptions(array($name => $value))->get($get, $params);
	// pass key value pairs containing the CURL options

curl's People

Contributors

hackerone avatar lmagalhaes avatar mauro-moreno avatar paillechat avatar sannek8552 avatar tatenen avatar tpruvot avatar waleoyediran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

curl's Issues

_isAssoc method doesn't exist

public function setHeaders($header)
    {
        if($this->_isAssoc($header)){
            $out = array();
            foreach($header as $k => $v){
                $out[] = $k .': '.$v;
            }
            $header = $out;
        }
        $this->setOption(CURLOPT_HTTPHEADER, $header);
        return $this;
    }

The method _isAssoc does not exist.

you forgot finish addHeader function

public function addHeader($header = [])
{
    $h = $this->request_options[CURLOPT_HTTPHEADER] ? $this->request_options[CURLOPT_HTTPHEADER] : [];
    foreach($header as $key => $val){
        $h[] =
    }

    $this->request_options[CURLOPT_HTTPHEADER] = $h;
}

Error sending array through POST

Hello
There was a problem with the method of POST, when I send an array of error takes off.
Helps formatting data to be sent via function http_build_query


public function post($url, $data, $params = array(), $debug = false)
{
        $url = $this->buildUrl($url, $params);
        $options = $this->getOptions();
        $options[CURLOPT_POST] = true;
        $options[CURLOPT_POSTFIELDS] = is_array($data) ? http_build_query($data) : $data;
        return $this->exec($url, $options, $debug);
}

getHeaders() returns wrong data

on my configuration $curl->getHeaders() returns wrong data.

this is happens because in $this->response (where you are storing response data)
contained something like this:

HTTP 100 Continue
\r\n\r\n
HTTP 100 Continue
\r\n\r\n
HTTP 200 OK
<..other headers..>
\r\n\r\n
<..body..>

solution is to rewrite getHeaders as following:

$responseEntries=explode("\r\n\r\n",$this->response);
$countRE = count($responseEntries);
$headerText = $responseEntries[$countRE-2];
$headers     = array();
foreach (explode("\r\n", $headerText) as $i => $line) {
   if ($i === 0) {
    $headers['http_code'] = $line;
   } else {
    list ($key, $value) = explode(': ', $line);
    $headers[$key] = $value;
   }
}
return $headers;

Missing port number on GET

Hi, thank you for your job,
it seems like builUrl() doesn't add port number (if specified) while returning the URL.

Row 83:
return $parsed['scheme'].'://'.$parsed['host'].$parsed['path'].$parsed['query'];

adding follow_location emulation

do it for self


private $_config = array(
    CURLOPT_HEADER => true,
    CURLOPT_FOLLOWLOCATION => false,
    ...
}
private $follow_location = false;    

private function canFollow() {
        return (!ini_get('open_basedir') and !ini_get('safe_mode'));
}

public function follow($follow) {
    $this->follow_location = $follow;
    return $this;
}

private function _exec($url){
       $this->setOption(CURLOPT_URL, $url);

       # redirects
       if($this->follow_location) {
        if($this->canFollow()) {
             $this->setOption(CURLOPT_FOLLOWLOCATION, true);
        }
    else {
        $this->setOption(CURLOPT_FOLLOWLOCATION, false);
    }
       }

    $response = curl_exec($this->_ch);
    $header_size = curl_getinfo($this->_ch, CURLINFO_HEADER_SIZE);
    $header = mb_substr($response, 0, $header_size);
    $content = mb_substr($response, $header_size);

    # redirects
    if($this->follow_location) {
        $http_code = curl_getinfo($this->_ch, CURLINFO_HTTP_CODE);
        if(in_array($http_code, array(301, 302, 303))) {
            if(preg_match('/Location: (.+?)\n/', $header, $matches)) {
                $url = trim($matches[1]);
                return $this->_exec($url);
            }
        }
    }

    if(!curl_errno($this->_ch))
        return $content;
    else
        throw new CException(curl_error($this->_ch));
}

    public function getRealUrl($url) {
        $this->follow(true);
        $this->setOption(CURLOPT_URL, $url);
        $this->setOption(CURLOPT_NOBODY, true);
        $this->setOption(CURLOPT_FOLLOWLOCATION, false);
        $header = curl_exec($this->_ch);
        if(preg_match('/Location: (.+?)\n/', $header, $matches)) {
            $url = trim($matches[1]);
            return $this->getRealUrl($url);
        }
        return $url;
    }

curl_close problem

code:

// initialize curl
public function init(){
    try{
        $this->_ch = curl_init();
        $options = is_array($this->options)?array_merge($this->_config,$this->options):$this->_config;
        $this->setOptions($options);

        // close curl on exit
        Yii::app()->onEndRequest = function(){
            curl_close($this->_ch);
        };
    }catch(Exception $e){
        throw new CException('Curl not installed');
    }
}

have a problem near "curl_close($this->_ch);" and throw php fatal error - "Using $this when not in object context". I replace it to:

public function init(){
    try{
        $this->_ch = curl_init();
        $options = is_array($this->options)?array_merge($this->_config,$this->options):$this->_config;
        $this->setOptions($options);
        $ch = $this->_ch;

        // close curl on exit
        Yii::app()->onEndRequest = function() use(&$ch){
            curl_close($ch);
        };
    }catch(Exception $e){
        throw new CException('Curl not installed');
    }
}

and everything became working fine. please fix.

There is no composer.json

When I try to add this library with composer, I get:

  [Composer\Repository\InvalidRepositoryException]                                                                                   
  No valid composer.json was found in any branch or tag of https://github.com/hackerone/curl.git, could not load a package from it.  

wrong settings merge

must be $this->options + $this->_config to allow rewrite default $_config options for example
'curl' => array(
'class' => 'ext.Curl',
'options' => array(
CURLOPT_FOLLOWLOCATION => (!ini_get('open_basedir') and !ini_get('safe_mode')) ? true : false,
),
),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.