372 lines
34 KiB
HTML
372 lines
34 KiB
HTML
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Sending data to a graph script with URI arguments (GET and POST)</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch13.html" title="Chapter 13. Getting hold of the data to be displayed"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Sending data to a graph script with URI arguments (GET and POST)</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 13. Getting hold of the data to be displayed</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Sending data to a graph script with URI arguments (GET and POST)"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2538267"></a>Sending data to a graph script with URI arguments (GET and POST)</h2></div></div></div>
|
||
|
||
<p>In order to send data between different HTML pages (or to a specific page) there
|
||
are two ways to do this. Either by including the parameters in the URI directly,
|
||
(e.g. <code class="code">http://localhost/mygraph.php?d1=12</code>) or by creating a POST request
|
||
and send to a page. The details of these two methods are discussed below.</p>
|
||
<p>
|
||
</p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3>
|
||
<p>There might be some confusion when to use the <code class="code">'&'</code> and
|
||
when to use <code class="code">'&amp;'</code> to separate variables in GET or POST
|
||
requests.</p>
|
||
<p>When writing URL in HTML the string should always be written with a full
|
||
entity encoding since the <code class="code">'&'</code> is a special character
|
||
indicating the start of an entity that ends with an <code class="code">';'</code>. So for
|
||
example writing a URI in a HTML page should look like.</p>
|
||
<p><code class="filename"><a
|
||
href="myscript.php?idx=1&amp;start=2009&amp;end=2010"></code></p>
|
||
<p>the browser will then correctly convert the URI to single
|
||
<code class="code">'&'</code> which is what should be sent to the server. When
|
||
typing URI directly in the browser (or in any plain text file) one should of
|
||
course always just use a single <code class="code">'&'</code>
|
||
</p>
|
||
</div><p>
|
||
</p>
|
||
<p>
|
||
</p><div class="tip" title="Tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Tip</h3>
|
||
<p>Use the method <code class="code">http_build_query()</code> to build http queries from
|
||
an array with keys and values.</p>
|
||
</div><p>
|
||
</p>
|
||
<div class="sect2" title="Using GET arguments"><div class="titlepage"><div><div><h3 class="title"><a name="id2538350"></a>Using GET arguments</h3></div></div></div>
|
||
|
||
<p>GET arguments are the arguments that can be added as part of the URI. For
|
||
example as</p>
|
||
<p>
|
||
</p><pre class="screen">http://localhost/mygraph.php?id=12&start=20081223&end=20090115</pre><p>
|
||
</p>
|
||
<p>PHP automatically places all given arguments in the "super global" array
|
||
variable <code class="code">$_GET</code> as an associative array.</p>
|
||
<p>There are a couple of things to note here</p>
|
||
<p>
|
||
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
|
||
<p>The values in the argument string in the URI must be URL encoded,
|
||
this can easily be done with the PHP function
|
||
<code class="code">urlencode()</code> (<code class="uri"><a class="uri" href="http://php.net/manual/en/function.urlencode.php" target="_top">See
|
||
PHP Manual</a></code>)</p>
|
||
</li><li class="listitem">
|
||
<p>When the arguments are read from $_GET the must be un-quoted with
|
||
a call to <code class="code">urldecode()</code></p>
|
||
</li><li class="listitem">
|
||
<p>Some browsers restrict the length of the URI that they will read
|
||
(typically < 2048bytes) which means that there is a limit on how
|
||
much data can be send as URI arguments</p>
|
||
</li><li class="listitem">
|
||
<p>Some browsers will allow the syntax
|
||
<code class="code">"a[0]=10&a[1]=11&a[2]=12"</code> i order to send
|
||
the array <code class="code">(10,11,12)</code></p>
|
||
</li></ul></div><p>
|
||
</p>
|
||
<p>This way of specifying the argument string is mostly useful when</p>
|
||
<p>
|
||
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
|
||
<p>The arguments are short</p>
|
||
</li><li class="listitem">
|
||
<p>When the user should be able to bookmark an URI</p>
|
||
</li><li class="listitem">
|
||
<p>When the data is not sensitive (since it can be seen in the
|
||
URI)</p>
|
||
</li><li class="listitem">
|
||
<p>When the graph should be viewable by clicking on a link (more on
|
||
this when we compare the GET method with the POST method
|
||
below)</p>
|
||
</li></ul></div><p>
|
||
</p>
|
||
<p>The "best practice" of using this method is to send a short key (or id) to the
|
||
graph script and the graph script itself will use this id to extract the real
|
||
data from a DB (or a plain text file). This way the same core graph script can
|
||
be used in various context to display wanted data.</p>
|
||
</div>
|
||
<div class="sect2" title="Using a POST request"><div class="titlepage"><div><div><h3 class="title"><a name="id2538499"></a>Using a POST request</h3></div></div></div>
|
||
|
||
<p>
|
||
</p><div class="warning" title="Warning" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Warning</h3>
|
||
<p>This is a fairly advanced topic and it is recommended to use the other
|
||
methods of sending data to a script unless the specifications explicitly
|
||
demands that a POST request is constructed. Furthermore this requires a
|
||
very good understanding of HTTP request headers and the difference
|
||
between server side and browser side so if you are not sure that you
|
||
have the necessary background we strongly recommend to stay away from
|
||
this method.</p>
|
||
</div><p>
|
||
</p>
|
||
<p>Two of the obvious restrictions with the GET method is that <span class="bold"><strong>a)</strong></span> the length of the data is restricted and <span class="bold"><strong>b)</strong></span> the data is visible directly in the URI. The other
|
||
way to send data as part of the HTTP request is to use the POST method. </p>
|
||
<p>Unfortunately this is not as easy as just doing some magic and then we get the
|
||
same functionality as with the GET method. Even some authors get this wrong in
|
||
some very prominent PHP text books. Unfortunately it will take us too far to
|
||
discuss all the details of HTTP request headers (as described in <code class="uri"><a class="uri" href="http://www.w3.org/Protocols/rfc2616/rfc2616.html" target="_top">RFC2616</a></code>)
|
||
but we will explain the very basics.</p>
|
||
<p>First. let's state what an HTTP request is</p>
|
||
<p>
|
||
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
|
||
<p>A POST HTTP request is used to send data to a specified URI from a
|
||
client. A client is normally a WEB-browser but can also (as we will
|
||
use it) be a script that sends the request to the same or to another
|
||
server. The data is URL encoded and passed in the body of the
|
||
request. There is no theoretically limit on the length of the
|
||
data.</p>
|
||
</li></ul></div><p>
|
||
</p>
|
||
<p>A further common misunderstanding is that it is possible to use the PHP method
|
||
<code class="code">header()</code> in order to create a POST request. This is even given
|
||
as an example in the notes to the <code class="code">header()</code> method in the PHP manual
|
||
(See <code class="uri"><a class="uri" href="http://php.net/manual/en/function.header.php" target="_top">PHP
|
||
Manual</a></code>). This is <span class="bold"><strong>wrong</strong></span>. It is
|
||
<span class="bold"><strong>not</strong></span> possible to use the
|
||
<code class="code">header()</code> method to send a POST header. Trying to do this
|
||
reveals a basic misunderstanding of the role of a server and client.</p>
|
||
<p>
|
||
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
|
||
<p>The <code class="code">header()</code> method is used to send one or more
|
||
headers from the server <span class="bold"><strong>to the
|
||
client</strong></span> (i.e. WEB-browser)</p>
|
||
</li><li class="listitem">
|
||
<p>The HTTP POST request goes from the client (i.e. WEB-browser)
|
||
<span class="bold"><strong>to the server</strong></span></p>
|
||
</li></ul></div><p>
|
||
</p>
|
||
<div class="figure"><a name="id2538657"></a><p class="title"><b>Figure 13.1. Post vs. header() data direction</b></p><div class="figure-contents">
|
||
|
||
<div class="mediaobject"><img src="images/post_vs_header.png" alt="Post vs. header() data direction"><div class="caption">
|
||
<p>Note that the image is greatly simplified to help illustrate the
|
||
vital point on data direction. For example the post request shown to
|
||
originate from a browser could originate from any client, for
|
||
example another script taking the role of a client.</p>
|
||
</div></div>
|
||
</div></div><br class="figure-break">
|
||
<p>Hence it is never possible to "simulate" a POST call with the use of the
|
||
<code class="code">header()</code> function. There are basically three (correct) ways to
|
||
simulate a POST request as described below.</p>
|
||
<p>Before we continue lets first recapitulate the most common use of a POST
|
||
request, i.e. in a HTML form submission. When data is entered in a form on a
|
||
HTML page and the user presses the "Submit" button the data from the form is
|
||
packed as a POST request which is sent to the server at the specified URI (the
|
||
action URI). The server will the reply back to the POST request (with the data
|
||
sent back from the target of the post request) and the browser will show the
|
||
reply in the window in place of the original HTML form page.</p>
|
||
<p>However there is a crucial difference when we do this manually from a script
|
||
(running on the server) compared with the original form post data from the
|
||
browser to the server. After issuing a POST request (originating from a HTML
|
||
form) the browser automatically replaces the current page with the reply from
|
||
the POST request as a "new" page (by default using the same target window as the
|
||
request was made from).</p>
|
||
<p>This is not possible to do when sending a "fake" post request to a page since
|
||
we are not the browser. Instead what we will see in the browser is the page
|
||
<span class="italic">sending the POST request</span>, and not the
|
||
target of the post request. The best we can accomplish is to show the reply
|
||
inline in the calling page which are then shown in the browser.</p>
|
||
<p>This means that it is not possible to create a POST request and then somehow
|
||
directly show the reply as the resulting image. Instead what we can do is to
|
||
send the data to a image script (via a POST header) and then the graph script
|
||
can write the image to a file that is accessible from the server.</p>
|
||
<p>So to summarize. What we can do with a post request is to send the data to a
|
||
script <code class="filename">b.php</code> from a script <code class="filename">a.php</code>. The
|
||
<code class="filename">b.php</code> can then execute some statements, for example
|
||
creating a graph and store it on the server. This stored image can later be read
|
||
by the <code class="filename">a.php</code> script, for example via a <code class="sgmltag-element"><img></code>
|
||
tag.</p>
|
||
<p><span class="bold"><strong>How to create a POST request</strong></span></p>
|
||
<p>There are in principle three ways of constructing a POST request to send data
|
||
to a specified URI as shown below. </p>
|
||
<p>After we have made the request (with any of the three methods shown below) the
|
||
server will reply back with the response created by the URI. This response is
|
||
any output sent by the script we are sending our request to. Normally this
|
||
should just be a return code indicating if the request was successful or
|
||
not.</p>
|
||
<p>Remember that these are calls made from the script <code class="filename">a.php</code>
|
||
running on server <span class="bold"><strong>A</strong></span> to a script
|
||
<code class="filename">b.php</code> running on server <span class="bold"><strong>B</strong></span>. There are no browser involved in these calls apart from the
|
||
initial request to run <code class="filename">a.php</code>. The figure below illustrates
|
||
the first phase when the request is sent to the B side</p>
|
||
<p>
|
||
</p><div class="figure"><a name="id2538810"></a><p class="title"><b>Figure 13.2. The request phase of a POST header</b></p><div class="figure-contents">
|
||
|
||
<div class="mediaobject"><img src="images/post_request_phase1.png" alt="The request phase of a POST header"><div class="caption">
|
||
<p>1. The request is initially made by calling the a.php script,
|
||
e.g. http://localhost/a.php in the browser</p>
|
||
<p>2. When the a.php script is executed it will create the POST
|
||
header and call script b.php o the server B (could possibly be
|
||
the same server). Since the browser is displaying script a.php
|
||
we can never change that directly but we can display the reply
|
||
from b.php in the page displayed by a.php</p>
|
||
</div></div>
|
||
</div></div><p><br class="figure-break">
|
||
</p>
|
||
<div class="figure"><a name="id2538834"></a><p class="title"><b>Figure 13.3. The reply phase of a POST request</b></p><div class="figure-contents">
|
||
|
||
<div class="mediaobject"><img src="images/post_request_phase2.png" alt="The reply phase of a POST request"><div class="caption">
|
||
<p>3. The b.php returns a reply by fir example echoing back a reply
|
||
code</p>
|
||
<p>4. The script running the browser receives its final data (which
|
||
is the reply from the b.php script) and then finish the original
|
||
request started in step 1.</p>
|
||
</div></div>
|
||
</div></div><br class="figure-break">
|
||
<p><span class="bold"><strong>Note:</strong></span> All the <code class="code">sendPostData_vX()</code>
|
||
methods below assumes that the data is already urlencoded.</p>
|
||
<p>
|
||
</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
|
||
<p><span class="bold"><strong>Create a stream request.</strong></span></p>
|
||
<p>The advantage with this method is that it is available by default
|
||
in PHP >= 4.3 and of course in PHP5,6 without the need to install
|
||
additional libraries.</p>
|
||
<p>
|
||
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16
|
||
17
|
||
18
|
||
19
|
||
20
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
||
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">sendPostData_v1</span><span class="hl-brackets">(</span><span class="hl-var">$url</span><span class="hl-code">, </span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-var">$opts</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http</span><span class="hl-quotes">'</span><span class="hl-code"> => </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-code">
|
||
</span><span class="hl-quotes">'</span><span class="hl-string">method</span><span class="hl-quotes">'</span><span class="hl-code"> => </span><span class="hl-quotes">'</span><span class="hl-string">POST</span><span class="hl-quotes">'</span><span class="hl-code">,
|
||
</span><span class="hl-quotes">'</span><span class="hl-string">header</span><span class="hl-quotes">'</span><span class="hl-code"> =>
|
||
</span><span class="hl-quotes">"</span><span class="hl-string">Content-type: application/x-www-form-urlencoded</span><span class="hl-special">\r</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">.
|
||
</span><span class="hl-quotes">"</span><span class="hl-string">Content-Length: </span><span class="hl-quotes">"</span><span class="hl-code">.</span><span class="hl-identifier">strlen</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">"</span><span class="hl-special">\r</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">.
|
||
</span><span class="hl-quotes">'</span><span class="hl-string">content</span><span class="hl-quotes">'</span><span class="hl-code"> => </span><span class="hl-var">$data</span><span class="hl-code">,
|
||
</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$stream</span><span class="hl-code"> = </span><span class="hl-identifier">stream_context_create</span><span class="hl-brackets">(</span><span class="hl-var">$opts</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$url</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">rb</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-reserved">false</span><span class="hl-code">, </span><span class="hl-var">$stream</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> Some error handling } </span><span class="hl-comment"></span><span class="hl-code">
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Find out what the page returns as its body </span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$reply</span><span class="hl-code"> = </span><span class="hl-identifier">stream_get_contents</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-var">$reply</span><span class="hl-code"> === </span><span class="hl-reserved">false</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> Some error handling } </span><span class="hl-comment"></span><span class="hl-code">
|
||
|
||
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-var">$reply</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div><p>
|
||
</p>
|
||
</li><li class="listitem">
|
||
<p><span class="bold"><strong>Open a socket directly and write to it
|
||
</strong></span></p>
|
||
<p>
|
||
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16
|
||
17
|
||
18
|
||
19
|
||
20
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
||
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">sendPostData_v2</span><span class="hl-brackets">(</span><span class="hl-var">$url</span><span class="hl-code">,</span><span class="hl-var">$data</span><span class="hl-code">,</span><span class="hl-var">$port</span><span class="hl-code">=</span><span class="hl-number">80</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-var">$errno</span><span class="hl-code">=-</span><span class="hl-number">1</span><span class="hl-code">;
|
||
</span><span class="hl-var">$errstr</span><span class="hl-code">=</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">;
|
||
</span><span class="hl-var">$fs</span><span class="hl-code"> = </span><span class="hl-identifier">fsockopen</span><span class="hl-brackets">(</span><span class="hl-var">$url</span><span class="hl-code">,</span><span class="hl-var">$port</span><span class="hl-code">,</span><span class="hl-var">$errno</span><span class="hl-code">,</span><span class="hl-var">$errstr</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$fs</span><span class="hl-code"> === </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> Some error handling } </span><span class="hl-comment"></span><span class="hl-code">
|
||
|
||
</span><span class="hl-var">$header</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">POST </span><span class="hl-var">$url</span><span class="hl-string"> HTTP/1.0</span><span class="hl-special">\r</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">;
|
||
</span><span class="hl-var">$header</span><span class="hl-code"> .= </span><span class="hl-quotes">"</span><span class="hl-string">Content-Type: application/x-www-form-urlencoded</span><span class="hl-special">\r</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">;
|
||
</span><span class="hl-var">$header</span><span class="hl-code"> .= </span><span class="hl-quotes">"</span><span class="hl-string">Content-Length: </span><span class="hl-quotes">"</span><span class="hl-code"> . </span><span class="hl-identifier">strlen</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code"> . </span><span class="hl-quotes">"</span><span class="hl-special">\r</span><span class="hl-special">\n</span><span class="hl-special">\r</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">;
|
||
</span><span class="hl-identifier">fputs</span><span class="hl-brackets">(</span><span class="hl-var">$fs</span><span class="hl-code">, </span><span class="hl-var">$header</span><span class="hl-code"> . </span><span class="hl-var">$data</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Find out what the page returns as its body </span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$reply</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">while</span><span class="hl-brackets">(</span><span class="hl-code"> !</span><span class="hl-identifier">feof</span><span class="hl-brackets">(</span><span class="hl-var">$fs</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-var">$reply</span><span class="hl-code"> .= </span><span class="hl-identifier">fgets</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">,</span><span class="hl-number">8192</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-var">$reply</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div><p>
|
||
</p>
|
||
</li><li class="listitem">
|
||
<p><span class="bold"><strong> Use CURL to handle all the necessary
|
||
details</strong></span></p>
|
||
<p><span class="bold"><strong>Note 1:</strong></span> This requires that curl
|
||
libraries are installed and that curl is enabled in the PHP
|
||
installations. </p>
|
||
<p><span class="bold"><strong>Note 2:</strong></span> Depending on the
|
||
application there might be many more options that needs to be
|
||
tweaked. The one used below are just the bare necessities.</p>
|
||
<p><span class="bold"><strong>Note 3:</strong></span> Using CURL is the most
|
||
general way to handle POST requests and simplifies the additional
|
||
complexity if we want to add encryption (i.e. HTTPS) in the
|
||
connection handling.</p>
|
||
<div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16
|
||
17
|
||
18
|
||
19
|
||
20
|
||
21
|
||
22
|
||
23
|
||
24
|
||
25
|
||
26
|
||
27
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
||
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">sendPostData_v3</span><span class="hl-brackets">(</span><span class="hl-var">$url</span><span class="hl-code">,</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">
|
||
</span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Initialize and get the curl handle</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$ch</span><span class="hl-code"> = </span><span class="hl-identifier">curl_init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Include possible headers in the reply from the server as well</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-identifier">curl_setopt</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$ch</span><span class="hl-code">, </span><span class="hl-identifier">CURLOPT_HEADER</span><span class="hl-code">, </span><span class="hl-reserved">true</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Return the reply as a string from curl_exec() instead of directly to stdout</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-identifier">curl_setopt</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$ch</span><span class="hl-code">, </span><span class="hl-identifier">CURLOPT_RETURNTRANSFER</span><span class="hl-code">, </span><span class="hl-reserved">true</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> The URI we want to send the post data to</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-identifier">curl_setopt</span><span class="hl-brackets">(</span><span class="hl-var">$ch</span><span class="hl-code">, </span><span class="hl-identifier">CURLOPT_URL</span><span class="hl-code">, </span><span class="hl-var">$url</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Use the POST method</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-identifier">curl_setopt</span><span class="hl-brackets">(</span><span class="hl-var">$ch</span><span class="hl-code">, </span><span class="hl-identifier">CURLOPT_POST</span><span class="hl-code">, </span><span class="hl-reserved">true</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> All the data to be sent</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-identifier">curl_setopt</span><span class="hl-brackets">(</span><span class="hl-var">$ch</span><span class="hl-code">, </span><span class="hl-identifier">CURLOPT_POSTFIELDS</span><span class="hl-code">,</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-var">$reply</span><span class="hl-code"> = </span><span class="hl-identifier">curl_exec</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-var">$ch</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-identifier">curl_close</span><span class="hl-brackets">(</span><span class="hl-var">$ch</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-var">$reply</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div>
|
||
</li></ol></div><p>
|
||
</p>
|
||
</div>
|
||
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch13.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>
|