php采用file_get_contents代替使用curl实例

yipeiwu_com6年前PHP代码库

本文实例讲述了php采用file_get_contents代替使用curl的方法,分享给大家供大家参考。具体实现方法如下:

file_get_contents代替使用curl其实不多见了,但有时你碰到服务器不支持curl时我们可以使用file_get_contents代替使用curl,下面看个例子。

当用尽一切办法发现 服务器真的无法使用curl时。或者curl不支持https时。curl https 出现502时。你又不想重装网站环境的时候,你就改用file_get_contents 代替吧。
curl 经常使用的 curl get curl post
curl get 替代 直接用file_get_contents($url) 就可以了
curl post 替代如下:

复制代码 代码如下:
function Post($url, $post = null) {      
        $content = http_build_query($post);
        $content_length = strlen($content);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' =>"Content-type: application/x-www-form-urlencoded",
                'content' => $post
            )
        );
        return file_get_contents($url, false, stream_context_create($options));
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

php基于 swoole 实现的异步处理任务功能示例

本文实例讲述了php基于 swoole 实现的异步处理任务功能。分享给大家供大家参考,具体如下: 安装swoole: 下载官方swoole压缩包,解压进入目录 $ cd swoole...

探究Laravel使用env函数读取环境变量为null的问题

探究Laravel使用env函数读取环境变量为null的问题

发现问题 在 Laravel 项目中,如果执行了 php artisan config:cache 命令把配置文件缓存起来后,在 Tinker 中(Tinker 是 Laravel 自带...

php获取一定范围内取N个不重复的随机数

本文实例讲述了php获取一定范围内取N个不重复的随机数的方法。分享给大家供大家参考,具体如下: //range 是将1000到9999 列成一个数组 $numbers = range...

解决file_get_contents无法请求https连接的方法

错误: Warning: fopen() [function.fopen]: Unable to find the wrapper "https" - did you forget to...

PHP对MongoDB[NoSQL]数据库的操作

一、MongoDB简介 MongoDB (名称来自"humongous") 是一个可扩展的、高性能、开源、模式自由、面向文档的数据库,集文档数据库、键值对存储和关系型数据库的优点于一身。...