当前位置:首页 > PHP教程 > PHP常见问题

PHP函数分享之curl方式取得数据、模拟登陆、POST数据

废话不多说直接上代码

复制代码 代码如下:

/********************** curl 系列 ***********************/
//直接通过curl方式取得数据(包含post、header等)
/*
 * $url: 如果非数组,则为http;如是数组,则为https
 * $header: 头文件
 * $post: post方式提交 array形式
 * $cookies: 0默认无cookie,1为设置,2为获取
 */
public function curl_allinfo($urls, $header = false, $post = false, $cookies = 0) {
    $url = is_array($urls) ? $urls['0'] : $urls;
    $ch = curl_init();
    curl_setopt($ch, curlopt_url, $url);
    curl_setopt($ch, curlopt_returntransfer, 1);

    //带header方式提交
    if($header != false){
        curl_setopt($ch, curlopt_httpheader, $header);
    }

    //post提交方式
    if($post != false){
        curl_setopt($ch, curlopt_post, 1);
        curl_setopt($ch, curlopt_postfields, $post);
    }

    if($cookies == 1){
        curl_setopt($ch, curlopt_cookiejar, "cookiefile");
    }else if($cookies == 2){
        curl_setopt($ch, curlopt_cookiefile, "cookiefile");
    }

    if(is_array($urls)){
        curl_setopt($ch, curlopt_ssl_verifypeer, false);
        curl_setopt($ch, curlopt_ssl_verifyhost, false);
    }

    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!