天天看点

【POST Requests】POST请求

如果只是想使用qr生成的而已,直接点击这里 -->  http://blog.csdn.net/eadio/article/details/25328011

原地址:

https://developers.google.com/chart/infographics/docs/post_requests

先声明不知怎么授权,只是想翻译转载过来的,如果不行的话,请告知,会删掉的。。。

本章介绍为什么以及怎样请求使用HTTP POST来请求一张图像

简介

======================================================================

如果你在代码里请求一张图像【requestingan image in code】,或者你需要一串长度超过2K的字符串,那么你需要使用HTTP POST来发送你的图像请求。该信息图表服务器支持HTTP POST请求高达16K长。

下面最基本的POST请求示例:使用<form>元素

<form action='https://chart.googleapis.com/chart' method='POST'>      
  <input type='hidden' name='cht' value='qr' />      
  <input type='hidden' name='chs' value='300x300' />      
  <input type='hidden' name='chl' value='This is my QR code' />      
  <input type='submit'  />      
</form>       
此图片实际上是托管在一个<iframe>的页面,下面是<form>代码:      
<form action='https://chart.googleapis.com/chart' method='POST'>      
  <input type='hidden' name='cht' value='qr' />      
  <input type='hidden' name='chs' value='300x300' />      
  <input type='hidden' name='chl' value='This is my QR code'/>      
  <input type='submit'  />      
</form>      
一个有效的POST请求响应得到的是一张PNG图像,其实和作为一个GET请求响应得到的是一样的      
提示:某些特殊浏览器缓存请求得到一个特定的URL,这样即使你改变了POST参数,但是实际上浏览器并没有重新查询图像服务器。当你试图重新加载一张经常变化的图像(也许是你在测试过程中的问题),这样就可能导致出现这种情况了。要解决这个问题,在每个请求值变化的POST URL的末尾添加参数?chid=value:【这个可能有点拗口,待会下面有例子可以好好观察,其实就是在https://chart.googleapis.com/chart后面添加参数而已】图像服务器将忽略这个参数,而浏览器则会重新发送查询而不在是简单的加载缓存中的版本。      
有许多的方式使用POST,所有这些都需要额外的编码无论是页面上编码还是在服务器上托管页面。要使用POST的话,你通常都会创建一个单独的页面来设置图像,然后在你的主页使用<iframe>或者是<img>标签来嵌入或者链接这些单独的页面,下面将为你介绍这些。      
下面是使用POST与javascript和PHP的例子      
使用javascript进行POST请求      
使用javascript POST请求的最简单的方法是创建一个将图像数据包含在<input>元素的表单页面,并让页面开启的同时自动发送POST请求,使用onload()事件处理,页面就会被PNG格式的图像所取代,承载此图像的页面必须使用<iframe>元素包括本页。下面是图像页面代码:      
注:下面的示例包含在URL中使用chid参数设置来改变值,这会导致浏览器刷新是为了解决上述提示中描述到的问题。但是如果你的图像并没有经常更新变化,你并不需要强制加上这个参数。      
post_infographic.html      

<htmlxmlns="http://www.w3.org/1999/xhtml">

 <head>

 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

 <script type='application/javascript'>

   // Send the POST when the page is loaded,

   // which will replace this whole page with the retrieved image.

   function loadGraph() {

     var frm = document.getElementById('post_form');

     if (frm) {

      frm.submit();

     }

    }

 </script>

 </head>

 <body οnlοad="loadGraph()">

   <form action='https://chart.googleapis.com/chart' method='POST'id='post_form'

         οnsubmit="this.action = 'https://chart.googleapis.com/chart?chid='+ (new Date()).getMilliseconds(); return true;">  <input type='hidden' name='cht' value='qr'/>

     <input type='hidden' name='cht' value='qr' />

     <input type='hidden' name='chs' value='300x300' />

     <input type='hidden' name='chl' value='This is my QR code' />

     <input type='submit'  />

   </form>

 </body>

</html>

如果你使用的是<form>元素的话,你并不需要URL来编码字符串。

此图像可以被加载到另外的页面上,在主页上通过使用<iframe>元素,像下面这样:

another_page.html

<iframesrc="post_infographic.html" width="300"height="200"></iframe>

使用PHP的POST请求

大多数服务器端语言支持显式的POST请求。这里是使用PHP做一个POST请求的例子。这个例子演示了随机生成150个随机值的QR生成码。要使用这个,你必须使用一个$qrcode的数组来包含图像数据。

注:下面的示例包含在URL中使用chid参数设置来改变值,这会导致浏览器刷新是为了解决上述提示中描述到的问题。但是如果你的图像并没有经常更新变化,你并不需要强制加上这个参数。

imageserver-image.php

<?php

  //Create some random text-encoded data for a QR code.

 header('content-type: image/png');

 $url = 'https://chart.googleapis.com/chart?chid=' . md5(uniqid(rand(),true));

 $chd = 't:';

  for($i = 0; $i < 150; ++$i) {

   $data = rand(0, 100000);

   $chd .= $data . ',';

  }

 $chd = substr($chd, 0, -1);

  //Add image type, image size, and data to params.

  $qrcode= array(

   'cht' => 'qr',

   'chs' => '300x300',

   'chl' => $chd);

  //Send the request, and print out the returned bytes.

 $context = stream_context_create(

   array('http' => array(

     'method' => 'POST',

     'content' => http_build_query($qrcode))));

 fpassthru(fopen($url, 'r', false, $context));

?>

嵌入该图像比使用javascript示例更简单,因为你可以单单使用<img>标签就能指向到你的POST页面,如下所示:

another_page.html

<img width='300' height='300'src='imageserver-image.php'>

继续阅读