[PHP学习教程

引言:经常在开发期间,客户端与服务端的调试都是借助于真实的容器返回。尤其是在处理到POST时,通常刚刚入门的兄弟姐妹就一定要借助容器。今天,我们就来处理一下模拟HTTP。

本文列举了常见的四种请求方式:

大家直接观看代码吧。

函数版本[file_get_contents]


基本信息:

  string send_post ( string $url, string $data )

参数:

  url-请求地址。

  data-POST键值对数据。

返回值:

  string-响应HTML内容。

<?php
/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $data post键值对数据
 * @return string
 */
function send_post($url, $data) {
    $postdata = http_build_query($data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间(单位:s)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
 
    return $result;
}

示例1:

$post_data = array(
    'username' => 'ac',
    'password' => 'superdo'
);
send_post('http://do.org.cn/login', $post_data);

实战经验:

当我利用上述代码给另一台服务器发送http请求时,发现,如果服务器处理请求时间过长,本地的PHP会中断请求,即所谓的超时中断,第一个怀疑的是PHP本身执行时间的超过限制,但想想也不应该,因为老早就按照这篇文章设置了“PHP执行时间限制”,仔细琢磨,想想,应该是http请求本身的一个时间限制,于是乎,就想到了怎么给http请求时间限制搞大一点。。。。。。查看PHP手册,果真有个参数 “ timeout ”,默认不知道多大,当把它的值设大一点,问题得已解决,弱弱地做个笔记~~~

函数版本[Socket]


基本信息:

  string send_socket ( string $remote_server, string $remote_path, string $post_string, int $port, int $timeout )

参数:

  remote_server-远程服务器。

  remote_path-远程路径。

  post_string-POST键值对数据。

  port-端口。

  timeout-超时时间。

返回值:

  string-响应HTML内容。

/**
 * 发送Socket请求
 * @param string $remote_server 远程服务器
 * @param string $remote_path 远程路径
 * @param string $post_string 请求数据
 * @param int $port 端口
 * @param int $timeout 超时时间
 * @return string 结果HTML内容
 *
 * 使用方法:
 *     $post_string = "app=socket&amp;version=beta";
 *     send_socket('do.org.cn', '/server.php', $post_string);
 */
function send_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30) {
    $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
    if (!$socket) die("$errstr($errno)");
    fwrite($socket, "POST $remote_path HTTP/1.0");
    fwrite($socket, "User-Agent: Socket Example");
    fwrite($socket, "HOST: $remote_server");
    fwrite($socket, "Content-type: application/x-www-form-urlencoded");
    fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
    fwrite($socket, "Accept:*/*");
    fwrite($socket, "");
    fwrite($socket, "mypost=$post_string");
    fwrite($socket, "");
    $header = "";
    while ($str = trim(fgets($socket, 4096))) {
        $header .= $str;
    }

    $data = "";
    while (!feof($socket)) {
        $data .= fgets($socket, 4096);
    }

    return $data;
}

函数版本[Curl]


基本信息:

  string send_curl ( string $url, string $post_string )

参数:

  url-请求网址。

  post_string-POST键值对数据。

返回值:

  string-响应HTML内容。

/**
 * 发送Curl请求
 * @param string $url 请求网址
 * @param string $post_string 请求数据
 *
 * 使用方法:
 *     $post_string = "app=request&version=beta";
 *     send_curl('http://do.org.cn/server.php', $post_string);
 */
function send_curl($url, $post_string) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "do.org.cn's CURL Example beta");
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

函数版本[Curl2]


基本信息:

  boolean send_request ( string $url, array $data, string $refererUrl, string $method, string $contentType, int $timeout, string $proxy )

参数:

  url-请求网址。

  data-发送数据。

  refererUrl-请求来源地址。

  method-请求方式 GET/POST。

  contentType-文档类型。

  timeout-超时时间。

  proxy-代理。

返回值:

  boolean-是否发送成功的布尔值。

/**
 * 发送HTTP请求
 *
 * @param string $url 请求网址
 * @param string $method 请求方式 GET/POST
 * @param string $refererUrl 请求来源地址
 * @param array $data 发送数据
 * @param string $contentType 文档类型
 * @param string $timeout 超时时间
 * @param string $proxy 代理
 * @return boolean
 */
function send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application/json', $timeout = 30, $proxy = false) {
    $ch = null;
    if ('POST' === strtoupper($method)) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER,0 );
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if ($refererUrl) {
            curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
        }
        if ($contentType) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
        }
        if (is_string($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
    } else if('GET' === strtoupper($method)) {
        if(is_string($data)) {
            $real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
        } else {
            $real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
        }

        $ch = curl_init($real_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if ($refererUrl) {
            curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
        }
    } else {
        $args = func_get_args();
        return false;
    }

    if ($proxy) {
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    $ret = curl_exec($ch);
    $info = curl_getinfo($ch);
    $contents = array(
            'httpInfo' => array(
                    'send' => $data,
                    'url' => $url,
                    'ret' => $ret,
                    'http' => $info,
            )
    );

    curl_close($ch);
    return $ret;
}

示例1:

调用 WCF接口 的一个例子:

$json = restRequest($r_url, 'POST', json_encode($data));

结束语


其实利用第三方框架,如PHPFetcher,PHPCrawl等都已经对页面请求做了封装,这里先不一一展开,大家请稍后观看后续编写的文章,谢谢大家的观看

未完,待更新...

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4824750.html

 

原文地址:https://www.cnblogs.com/superdo/p/4824750.html