note 123867 added to book.curl
| From: | tech dot myapp at gmail dot com | Date: | Tue, 21 May 2019 14:52:36 +0000 |
| Subject: | note 123867 added to book.curl | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-211095@lists.php.net to get a copy of this message | ||
/**
* @From: https://github.com/www-schat-top
* @Usage:
1)get
$rtv = Curl::go("https://??.com");
$rtv = Curl::go(["url"=>"https://??.com"]);
2)post
$rtv = Curl::go(["url"=>"https://??.com",
"data"=>["user"=>"john","age"=>28]]);
3)post json string
$rtv = Curl::go(["url"=>"https://??.com",
"data"=>json_encode(["user"=>"john","age"=>28]),
"json"=>true]);
==>
file_get_contents("php://input") //
'{"user":"john","age":20}'
json_decode(file_get_contents("php://input")) // ( [user] => john [age] =>
20 )
4)upload file and add headers
$rtv = Curl::go(["url"=>"https://??.com",
"data"=>["user"=>"john","age"=>28, 'file'
=> new \CURLFile(realpath("/tmp/2.tar.gz")),'max' => '20M',
'header'=>[....]]]);
==> $_POST['user'] = "john",
$_POST['age'] = 28
$_FILES['file']['name'] => 2.tar.gz
$_FILES['file']['type'] => application/octet-stream
$_FILES['file']['tmp_name'] => /tmp/phpW75BcK
$_FILES['file']['error'] => 0
$_FILES['file']['size'] => 6645
@return
[
"errno" => 0,
"error" => "",
"code" => 200,
"url" => "http://??.com/a.php",
"ip" => "127.0.0.1",
"header" => "
HTTP/1.1 200 OK
...
",
"body" => "....",
"info" => [
"url" => "http://schat.top",
"content_type" => "text/html; charset=UTF-8",
"http_code" => 200,
...
]
]
*/
class Curl {
public static function go($arr) {
if(!is_array($arr) && is_string($arr) &&
preg_match("/^https?:\/\//",$arr) == 1) {
$arr=["url"=>$arr];
}
$arr_fix = [
'data' => [],
'header'=> [],
'json' => false,
'max' => '20M',
'timeout' => 600,
];
$arr=array_merge($arr_fix,$arr);
ini_set('upload_max_filesize', $arr['max']);
ini_set('post_max_size', '20M');
ini_set('memory_limit', '128M');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $arr['url']);
$data = $arr['data'];
if(is_array($data) && count($data)>0 || is_string($data)){
curl_setopt($ch, CURLOPT_POST, 1);
if($arr['json'])
$arr['header'][] = 'Content-Type: application/json; charset=utf-8;';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$header_fix = [
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/71.0.3578.98 Safari/537.36',
];
$header = array_merge($header_fix,$arr['header']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, tmpfile());
$rtv = curl_exec($ch);
$errno = curl_errno($ch); // 0 is no error
$error = curl_error($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$resp_header = mb_substr($rtv,0,curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$body = mb_substr($rtv,curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$info = curl_getinfo($ch);
curl_close($ch);
return [
'errno' => $errno,
'error' => $error,
'code' => $code,
'url' => $info['url'],
'ip' => $info['primary_ip'],
'header' => $resp_header,
'body' => $body,
'info' => $info
];
}
}
----
Server IP: 208.43.231.9
Probable Submitter: 119.81.81.244 (proxied: 120.29.112.184)
----
Manual Page -- http://php.net/manual/en/book.curl.php
Edit -- https://master.php.net/note/edit/123867
Del: integrated -- https://master.php.net/note/delete/123867/integrated
Del: useless -- https://master.php.net/note/delete/123867/useless
Del: bad code -- https://master.php.net/note/delete/123867/bad+code
Del: spam -- https://master.php.net/note/delete/123867/spam
Del: non-english -- https://master.php.net/note/delete/123867/non-english
Del: in docs -- https://master.php.net/note/delete/123867/in+docs
Del: other reasons-- https://master.php.net/note/delete/123867
Reject -- https://master.php.net/note/reject/123867
Search -- https://master.php.net/manage/user-notes.php