php curl status code,php-curl各种请求

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值