php-curl各种请求
龙行 PHP 2019-6-13 1322 0评论
php模拟post请求 $data为数组
public function postFormData($uri,$data) {//$data为数组
$ch = curl_init (); //初始化curl
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 ); //使用post请求
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data); //提交数据
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true); //重定向地址也输出
$return = curl_exec ( $ch ); //得到返回值
curl_close ( $ch ); //关闭
return $return;exit;
}
php模拟post请求 $data_string为json
function http_post_data($url, $data_string) {//$data_string为json
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8",
"Content-Length: " . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
post方式提交请求 $data为string
/**
* 以get方式提交请求
* @param $url
* @return bool|mixed
*/
function httpGet($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
get方式提交请求
/**
* 以get方式提交请求
* @param $url
* @return bool|mixed
*/
function httpGet($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
模拟post
function curl_post($data, $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$r = curl_exec($ch);
curl_close($ch);
$r = json_decode($r, true);
return $r;
}
function curl_post($url, $data, $header, $post = 1) {
//初始化curl
$ch = curl_init();
//参数设置
$res = curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, $post);
if ($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
//连接失败
if ($result == FALSE) {
if ($this->BodyType == 'json') {
$result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}";
} else {
$result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>172001网络错误";
}
}
curl_close($ch);
return $result;
}
评论一下
赞助站长
赞助站长X
版权申明:此文如未标注转载均为本站原创,自由转载请表明出处《龙行博客》。
本文网址:https://www.liaotaoo.cn/248.html