[原创]让file_get_contents() 函数也做post,以及两种写法!

jackxiang 2009-2-6 11:54 | |
好像是由于我的curl_post有些问题,然后用到了如下函数的post:
我的curl_post:
  function curl_post($url, $content)
  {

    $str_url = $url;
    $str_post_data = $content;
    $ch_curl = curl_init ();
    curl_setopt ( $ch_curl, CURLOPT_TIMEOUT, 3 );
    curl_setopt ( $ch_curl, CURLOPT_HEADER, false );
    curl_setopt ( $ch_curl, CURLOPT_POST, 1 );
    curl_setopt ( $ch_curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch_curl, CURLOPT_URL, $str_url );
    curl_setopt ( $ch_curl, CURLOPT_POSTFIELDS, $str_post_data );
    $str_return = curl_exec ( $ch_curl );

    if ($str_return === false)
    return false;
    curl_close ( $ch_curl );
    return $str_return;
  }


首先贴上生产环境的代码段【建鑫编写】:
<?php
function request_post($url,$data,&$result)
{
    $ctx = array (
        'http' => array (
            'method' => 'POST',
            'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                . "Content-Length: " . strlen($data) . "\r\n",
            'content' => $data
            )
        );
       $context = stream_context_create($ctx);
      $result = file_get_contents($url,false,$context);
    if ($result === false){
      return false;
    }
    return true;
}
request_post("http://localhost","aa=aaa",$result);
echo $result;
?>

不过我对这个哥们用到ksort函数的file_get_contents()觉得不错:
function Post($url, $post = null)
{
    $context = array();

    if (is_array($post))
    {
        ksort($post);

        $context['http'] = array
        (
            'method' => 'POST',
            'content' => http_build_query($post, '', '&'),
        );
    }

    return file_get_contents($url, false, stream_context_create($context));
}

$data = array
(
    'name' => 'test',
    'email' => 'test@gmail.com',
    'submit' => 'submit',
);

echo Post('http://www.url.com/to/submit.php', $data);







转:



一直以为file_get_contents() 函数大概只能在请求一个url时做get操作,其实还有下面用法,可以让file_get_contents() 函数也做post:
<?php
// 1. 通过设置context来是file_get_contents()函数完成post操作,其实还可以设置很多的东西
// 设置方法1 ================================================================
$opts = array(
    'http'=>array(
        'method'=>'POST',
        'content'=>'a=b&c=d'
    )

);
$context = stream_context_create($opts);

// 设置方法2 ================================================================
/**
$context = stream_context_create(array()); // 参数必须是一个数组,允许是空数组
stream_context_set_option ($context,'http','method','POST'); // 第一个参数必须是一个stream或由stream_context_create产生的一个stream的上下文
stream_context_set_option ($context,'http','content','a=b&c=d&e=f');
*/

// =================================================================================

echo file_get_contents('http://ljj.cn/test.php',false,$context);

exit;
?>



file_get_content post的第一种写法:

$data = "ms=BE&iso=BE&vat=0462569145&BtnSubmitVat=Verify";//参数
$opts = array('http' => array (
'method'=>"POST",
'header'=>"Accept-language: en\r\n" .
"Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
//file_get_content第三个参数是资源类型,所以要先把数组转换成资源类型
//stream_context_create 第一个参数必须是二唯数组,第二个数组必须是一唯数组
$context = stream_context_create($opts);
$content = file_get_contents('xxxx', false, $opts);
echo $content;
urlencode() 如果url地址有空格,就要使用


file_get_content post的第二种写法:
$data = array(
'ms'=>'BE',
'iso'=>'BE',
'vat'=>'0462569145',
'BtnSubmitVat'=>'Verify',
);

$options = array('http'=>array(
'method'=>"POST",
'header'=>
"Accept-language: en\r\n".
"Content-type: application/x-www-form-urlencoded\r\n",
'content'=>http_build_query($data)//http_build_query是把数组转换为字符传的形式.结果形式:ms=BE&iso=BE&vat=0462569145&BtnSubmitVat=Verify,版本为php5才有
));
$context = stream_context_create($options);
$content = file_get_contents('xxx', false, $context);

file_get_content get的写法:
$content = file_get_contents('xxx?ms=BE&iso=BE&vat=0462569145&BtnSubmitVat=Verify');

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/1611/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2009-2-6 12:05
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]