码迷,mamicode.com
首页 > Web开发 > 详细

PHP Simulation HTTP Request(undone)

时间:2014-12-29 10:22:45      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

目录

0. 引言
1. file_get_contents版本
2. Socket版本
3. Curl版本
4. Curl版本(2)

 

0. 引言

本文总结了通过PHP代码方式模拟各种HTTP请求

 

1. file_get_contents版本

<?php
/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
function send_post($url, $post_data) 
{ 
    //使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串
    $postdata = http_build_query($post_data);
    $options = array(
        http => array(
            method => POST,
            header => Content-type:application/x-www-form-urlencoded,
            content => $postdata,
            timeout => 15 * 60 // 超时时间(单位:s)
        )
    );
    //创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    return $result;
}

$post_data = array(
    username => zhenghan,
    password => 111
);
$result = send_post(http://localhost/test/index.php, $post_data);
echo $result;

?>

Relevant Link:

http://php.net/manual/zh/function.http-build-query.php
http://php.net/manual/zh/function.stream-context-create.php

 

2. Socket版本

<?php
/**
 * Socket版本 
 */
function request_by_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;
}

$post_string = "app=socket&amp;version=beta"; 
$result = request_by_socket(localhost, /test.php, $post_string);

echo $result;

?>

Relevant Link:

http://php.net/manual/zh/function.fsockopen.php

 

3. Curl版本

<?php
/**
 * Curl版本
 */
function request_by_curl($remote_server, $post_string) 
{
    //初始化一个新的会话,返回一个cURL句柄,供curl_setopt(), curl_exec()和curl_close() 函数使用。
    $ch = curl_init();
    //curl_setopt — 设置一个cURL传输选项
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "http://littlehann.cnblogs.com CURL Example beta");
    //curl_exec — 执行一个cURL会话,这个函数应该在初始化一个cURL会话并且全部的选项都被设置后被调用。
    $data = curl_exec($ch);
    //关闭一个cURL会话并且释放所有资源。cURL句柄ch 也会被释放。
    curl_close($ch);

    return $data;
}

$post_string = "app=request&version=beta";
$result = request_by_curl(http://localhost/test.php, $post_string);
echo $result;

?>

Relevant Link:

http://php.net/manual/zh/function.curl-init.php
http://php.net/manual/zh/function.curl-setopt.php
http://php.net/manual/zh/function.curl-exec.php
http://blog.51yip.com/php/1039.html

 

4. Curl版本(2)

<?php
/**
 * 发送HTTP请求
 *
 * @param string $url 请求地址
 * @param string $method 请求方式 GET/POST
 * @param string $refererUrl 请求来源地址
 * @param array $data 发送数据
 * @param string $contentType 
 * @param string $timeout
 * @param string $proxy 
 */
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 $contents;
}

$data = array(1 => "hello world!");
$r_url = "http://localhost/test.php";
$result = send_request($r_url, json_encode($data), NULL, POST);
echo $result;

?>

Relevant Link:

http://php.net/manual/zh/function.curl-getinfo.php
http://blog.snsgou.com/post-161.html
http://www.cnblogs.com/simpman/p/3549816.html

 

Copyright (c) 2014 LittleHann All rights reserved

 

PHP Simulation HTTP Request(undone)

标签:

原文地址:http://www.cnblogs.com/LittleHann/p/4191034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!