hackerone / curl Goto Github PK
View Code? Open in Web Editor NEWCurl wrapper for Yii framework
Curl wrapper for Yii framework
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,
),
),
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);
}
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;
}
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.
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;
}
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'];
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;
Screen http://joxi.ru/GxW0UxjKTJCvNZQ0RYo
Options:
// Curl wrapper extension
'curl' => array(
'class' => 'ext.curl.Curl',
'options' => array(
/**/
),
),
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.
Sending GET request with array to ES gives error. Unpleasant problem
Unsupported operand types Curl.php(88)
$params = (isset($parsed['query']) ) ? $data + $parsed['query'] : $data;
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.