龙行博客

走路看风景,经历看人生,岁月留痕迹,人生留轨迹,17的历史,18的豪情,时间的匆忙,人生的风景,放开心胸往前走,成功再远行,放开理想往前走,梦想再行动。
现在位置:首页 > 编程语言 > PHP > php-curl各种请求

php-curl各种请求

龙行    PHP    2019-6-13    93    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\"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>";
        }
    }

    curl_close($ch);
    return $result;
}

评论一下 分享本文 赞助站长

赞助站长X

扫码赞助站长
联系站长
龙行博客
  • 版权申明:此文如未标注转载均为本站原创,自由转载请表明出处《龙行博客》。
  • 本文网址:https://www.liaotaoo.cn/248.html
  • 上篇文章:对短信验证码发送次数的限制,用PHP是如何实现
  • 下篇文章:有意思的phpweb目录管理工具(不是软件)
  • php
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论
快捷导航
联系博主
在线壁纸
给我留言
光羽影视
音乐欣赏
返回顶部