zoukankan      html  css  js  c++  java
  • file_get_contents()实现get+post请求

    先看file_get_contents()的定义:

    string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )

    其中$filename可以传文件的相对/绝对地址,也可以传url。

    简单Get请求:

    $ret = file_get_contents("http://www.baidu.com");

    复杂Get+Post请求:

    $url = '***';
    $data = ['username' => '***','password' => '***'];
    $data = json_encode($data);
    $opts = [
                     'http' => [
                                        'method' => 'POST',
                                        'header'  => 'Content-type:application/json',
                                        'content' => $data,
                               ]
            ];
    $context = stream_context_create($opts);
    $ret = file_get_contents($url,false,$context);
  • 相关阅读:
    设计模式总结
    设计模式之工厂
    C#
    UML画图总结
    UML视频总结
    类图
    读取文件信息
    HMAC算法加密
    SHA_1计算消息摘要
    获取指定长度的随机字符串
  • 原文地址:https://www.cnblogs.com/purelightme/p/6574154.html
Copyright © 2011-2022 走看看