本文目录一览:
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);
}
php 中发送get请求,后台无法获取
针对你说的方式,我分两个来回答,第一种方法,你把后台代码那个echo 'get ok'去掉,然后把前台发送的那个注释去掉,应该就能跳转了;第二种方法,你只是接收了参数,又没有返回任何值,当然判断不了是否又没有接收成功,你应该随便返回一个值,证明代码是能走通的,思路是这样,希望对你有帮助-php发送curl的get请求
Linux系统中curl get、post请求
一:curl get请求
二:curl post请求
备注:PHP中可以使用http_build_query()函数,处理curl post参数,使其支持多维数组传递
转自: