curlget(curlget请求保存文件)

http://www.itjxue.com  2023-02-13 14:17  来源:未知  点击次数: 

curl命令行 get/post请求

GET请求:

curl -X GET --header'Accept: application/json'';content=XXX'

POST请求:

curl -X POST --header'Content-Type: application/json'--header'Accept: application/json'-d'{"phone":"XXX","content":"XXX"}'''

Linux系统中curl get、post请求

一:curl get请求

二:curl post请求

备注:PHP中可以使用http_build_query()函数,处理curl post参数,使其支持多维数组传递

转自:

curl发送get请求带param

curl发送get请求带param操作如下。

1、直接发,可以测网络通不通。

2、使用-d可以忽略-XPOST。

怎么获取通过curl get方式获取的数据

因为,PHP CURL库默认1024字节的长度不等待数据的返回,所以你那段代码需增加一项配置:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

给你一个更全面的封装方法:

function req_curl($url, $status = null, $options = array())

{

$res = '';

$options = array_merge(array(

'follow_local' = true,

'timeout' = 30,

'max_redirects' = 4,

'binary_transfer' = false,

'include_header' = false,

'no_body' = false,

'cookie_location' = dirname(__FILE__) . '/cookie',

'useragent' = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',

'post' = array() ,

'referer' = null,

'ssl_verifypeer' = 0,

'ssl_verifyhost' = 0,

'headers' = array(

'Expect:'

) ,

'auth_name' = '',

'auth_pass' = '',

'session' = false

) , $options);

$options['url'] = $url;

$s = curl_init();

if (!$s) return false;

curl_setopt($s, CURLOPT_URL, $options['url']);

curl_setopt($s, CURLOPT_HTTPHEADER, $options['headers']);

curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options['ssl_verifypeer']);

curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options['ssl_verifyhost']);

curl_setopt($s, CURLOPT_TIMEOUT, $options['timeout']);

curl_setopt($s, CURLOPT_MAXREDIRS, $options['max_redirects']);

curl_setopt($s, CURLOPT_RETURNTRANSFER, true);

curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options['follow_local']);

curl_setopt($s, CURLOPT_COOKIEJAR, $options['cookie_location']);

curl_setopt($s, CURLOPT_COOKIEFILE, $options['cookie_location']);

if (!empty($options['auth_name']) is_string($options['auth_name']))

{

curl_setopt($s, CURLOPT_USERPWD, $options['auth_name'] . ':' . $options['auth_pass']);

}

if (!empty($options['post']))

{

curl_setopt($s, CURLOPT_POST, true);

curl_setopt($s, CURLOPT_POSTFIELDS, $options['post']);

//curl_setopt($s, CURLOPT_POSTFIELDS, array('username' = 'aeon', 'password' = '111111'));

}

if ($options['include_header'])

{

curl_setopt($s, CURLOPT_HEADER, true);

}

if ($options['no_body'])

{

curl_setopt($s, CURLOPT_NOBODY, true);

}

if ($options['session'])

{

curl_setopt($s, CURLOPT_COOKIESESSION, true);

curl_setopt($s, CURLOPT_COOKIE, $options['session']);

}

curl_setopt($s, CURLOPT_USERAGENT, $options['useragent']);

curl_setopt($s, CURLOPT_REFERER, $options['referer']);

$res = curl_exec($s);

$status = curl_getinfo($s, CURLINFO_HTTP_CODE);

curl_close($s);

return $res;

}

php Curl的get和post方法

get方法

function http_get($url)

{

? ? $oCurl = curl_init();

? ? if (stripos($url, "https://") !== FALSE) {

? ? ? ? curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);

? ? ? ? curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);

? ? ? ? //curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);

? ? ? ? //CURL_SSLVERSION_TLSv1

? ? }

? ? curl_setopt($oCurl, CURLOPT_URL, $url);

? ? curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);

? ? $sContent = curl_exec($oCurl);

? ? $aStatus = curl_getinfo($oCurl);

? ? curl_close($oCurl);

? ? if (intval($aStatus["http_code"]) == 200) {

? ? ? ? return $sContent;

? ? } else {

? ? ? ? return false;

? ? }

}

post方法

? ? // curlpost请求

? ? function http_post($url, $data = NULL, $json = false)

? ? {

? ? ? ? $curl = curl_init();

? ? ? ? curl_setopt($curl, CURLOPT_URL, $url);

? ? ? ? curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

? ? ? ? curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

? ? ? ? if (!empty($data)) {

? ? ? ? ? ? if ($json is_array($data)) {

? ? ? ? ? ? ? ? $data = json_encode($data);

? ? ? ? ? ? }

? ? ? ? ? ? curl_setopt($curl, CURLOPT_POST, 1);

? ? ? ? ? ? curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

? ? ? ? ? ? if ($json) { //发送JSON数据

? ? ? ? ? ? ? ? curl_setopt($curl, CURLOPT_HEADER, 0);

? ? ? ? ? ? ? ? curl_setopt(

? ? ? ? ? ? ? ? ? ? $curl,

? ? ? ? ? ? ? ? ? ? CURLOPT_HTTPHEADER,

? ? ? ? ? ? ? ? ? ? array(

? ? ? ? ? ? ? ? ? ? ? ? 'Content-Type: application/json; charset=utf-8',

? ? ? ? ? ? ? ? ? ? ? ? 'Content-Length:' . strlen($data)

? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? );

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

? ? ? ? $res = curl_exec($curl);

? ? ? ? $errorno = curl_errno($curl);

? ? ? ? if ($errorno) {

? ? ? ? ? ? return array('errorno' = false, 'errmsg' = $errorno);

? ? ? ? }

? ? ? ? curl_close($curl);

? ? ? ? return json_decode($res, true);

? ? }

(责任编辑:IT教学网)

更多

推荐CMS技巧文章