file_get_contents版本: 1 /* * 2 * 发送post请求 3 * @param string $url 请求地址 4 * @param array $post_data post键值对数据 5 * @return string 6 */ 7 function send_post( $url , $post_data ) { 8 9 $postdata = http_build_query ( $post_data );
file_get_contents版本:
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * 发送post请求 </span><span> 3</span> <span> * @param string $url 请求地址 </span><span> 4</span> <span> * @param array $post_data post键值对数据 </span><span> 5</span> <span> * @return string </span><span> 6</span> <span>*/</span> <span> 7</span> <span>function</span> send_post(<span>$url</span>, <span>$post_data</span><span>) { </span><span> 8</span> <span> 9</span> <span>$postdata</span> = <span>http_build_query</span>(<span>$post_data</span><span>); </span><span>10</span> <span>$options</span> = <span>array</span><span>( </span><span>11</span> 'http' =>; <span>array</span><span>( </span><span>12</span> 'method' =>; 'POST', <span>13</span> 'header' =>; 'Content-type:application/x-www-form-urlencoded', <span>14</span> 'content' =>; <span>$postdata</span>, <span>15</span> 'timeout' =>; 15 * 60 <span>//</span><span> 超时时间(单位:s)</span> <span>16</span> <span> ) </span><span>17</span> <span> ); </span><span>18</span> <span>$context</span> = <span>stream_context_create</span>(<span>$options</span><span>); </span><span>19</span> <span>$result</span> = <span>file_get_contents</span>(<span>$url</span>, <span>false</span>, <span>$context</span><span>); </span><span>20</span> <span>21</span> <span>return</span> <span>$result</span><span>; </span><span>22</span> }
使用如下:
<span>1</span> post_data = <span>array</span><span>( </span><span>2</span> 'username' => 'stclair2201', <span>3</span> 'password' => 'handan' <span>4</span> <span>); </span><span>5</span> send_post('http://blog.snsgou.com', <span>$post_data</span>);
实战经验:
当我利用上述代码给另一台服务器发送http请求时,发现,如果服务器处理请求时间过长,本地的PHP会中断请求,即所谓的超时中断,第一个怀疑的是PHP本身执行时间的超过限制,但想想也不应该,因为老早就按照这篇文章设置了“PHP执行时间限制”(【推荐】PHP上传文件大小限制大全 ),仔细琢磨,想想,应该是http请求本身的一个时间限制,于是乎,就想到了怎么给http请求时间限制搞大一点。。。。。。查看PHP手册,果真有个参数 “ timeout ”,默认不知道多大,当把它的值设大一点,问题得已解决,弱弱地做个笔记~~~
Socket版本:
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * Socket版本 </span><span> 3</span> <span> * 使用方法: </span><span> 4</span> <span> * $post_string = "app=socket&version=beta"; </span><span> 5</span> <span> * request_by_socket('blog.snsgou.com', '/restServer.php', $post_string); </span><span> 6</span> <span>*/</span> <span> 7</span> <span>function</span> request_by_socket(<span>$remote_server</span>,<span>$remote_path</span>,<span>$post_string</span>,<span>$port</span> = 80,<span>$timeout</span> = 30<span>) { </span><span> 8</span> <span>$socket</span> = <span>fsockopen</span>(<span>$remote_server</span>, <span>$port</span>, <span>$errno</span>, <span>$errstr</span>, <span>$timeout</span><span>); </span><span> 9</span> <span>if</span> (!<span>$socket</span>) <span>die</span>("<span>$errstr</span>(<span>$errno</span>)"<span>); </span><span>10</span> <span>fwrite</span>(<span>$socket</span>, "POST <span>$remote_path</span> HTTP/1.0"<span>); </span><span>11</span> <span>fwrite</span>(<span>$socket</span>, "User-Agent: Socket Example"<span>); </span><span>12</span> <span>fwrite</span>(<span>$socket</span>, "HOST: <span>$remote_server</span>"<span>); </span><span>13</span> <span>fwrite</span>(<span>$socket</span>, "Content-type: application/x-www-form-urlencoded"<span>); </span><span>14</span> <span>fwrite</span>(<span>$socket</span>, "Content-length: " . (<span>strlen</span>(<span>$post_string</span>) + 8) . ""<span>); </span><span>15</span> <span>fwrite</span>(<span>$socket</span>, "Accept:*/*"<span>); </span><span>16</span> <span>fwrite</span>(<span>$socket</span>, ""<span>); </span><span>17</span> <span>fwrite</span>(<span>$socket</span>, "mypost=<span>$post_string</span>"<span>); </span><span>18</span> <span>fwrite</span>(<span>$socket</span>, ""<span>); </span><span>19</span> <span>$header</span> = ""<span>; </span><span>20</span> <span>while</span> (<span>$str</span> = <span>trim</span>(<span>fgets</span>(<span>$socket</span>, 4096<span>))) { </span><span>21</span> <span>$header</span> .= <span>$str</span><span>; </span><span>22</span> <span> } </span><span>23</span> <span>24</span> <span>$data</span> = ""<span>; </span><span>25</span> <span>while</span> (!<span>feof</span>(<span>$socket</span><span>)) { </span><span>26</span> <span>$data</span> .= <span>fgets</span>(<span>$socket</span>, 4096<span>); </span><span>27</span> <span> } </span><span>28</span> <span>29</span> <span>return</span> <span>$data</span><span>; </span><span>30</span> }
Curl版本:
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * Curl版本 </span><span> 3</span> <span> * 使用方法: </span><span> 4</span> <span> * $post_string = "app=request&version=beta"; </span><span> 5</span> <span> * request_by_curl('http://blog.snsgou.com/restServer.php', $post_string); </span><span> 6</span> <span>*/</span> <span> 7</span> <span>function</span> request_by_curl(<span>$remote_server</span>, <span>$post_string</span><span>) { </span><span> 8</span> <span>$ch</span> =<span> curl_init(); </span><span> 9</span> curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>$remote_server</span><span>); </span><span>10</span> curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, 'mypost=' . <span>$post_string</span><span>); </span><span>11</span> curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>); </span><span>12</span> curl_setopt(<span>$ch</span>, CURLOPT_USERAGENT, "snsgou.com's CURL Example beta"<span>); </span><span>13</span> <span>$data</span> = curl_exec(<span>$ch</span><span>); </span><span>14</span> curl_close(<span>$ch</span><span>); </span><span>15</span> <span>16</span> <span>return</span> <span>$data</span><span>; </span><span>17</span> }
Curl版本(2)
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * 发送HTTP请求 </span><span> 3</span> <span> * </span><span> 4</span> <span> * @param string $url 请求地址 </span><span> 5</span> <span> * @param string $method 请求方式 GET/POST </span><span> 6</span> <span> * @param string $refererUrl 请求来源地址 </span><span> 7</span> <span> * @param array $data 发送数据 </span><span> 8</span> <span> * @param string $contentType </span><span> 9</span> <span> * @param string $timeout </span><span>10</span> <span> * @param string $proxy </span><span>11</span> <span> * @return boolean </span><span>12</span> <span>*/</span> <span>13</span> <span>function</span> send_request(<span>$url</span>, <span>$data</span>, <span>$refererUrl</span> = '', <span>$method</span> = 'GET', <span>$contentType</span> = 'application/json', <span>$timeout</span> = 30, <span>$proxy</span> = <span>false</span><span>) { </span><span>14</span> <span>$ch</span> = <span>null</span><span>; </span><span>15</span> <span>if</span>('POST' === <span>strtoupper</span>(<span>$method</span><span>)) { </span><span>16</span> <span>$ch</span> = curl_init(<span>$url</span><span>); </span><span>17</span> curl_setopt(<span>$ch</span>, CURLOPT_POST, 1<span>); </span><span>18</span> curl_setopt(<span>$ch</span>, CURLOPT_HEADER,0<span> ); </span><span>19</span> curl_setopt(<span>$ch</span>, CURLOPT_FRESH_CONNECT, 1<span>); </span><span>20</span> curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, 1<span>); </span><span>21</span> curl_setopt(<span>$ch</span>, CURLOPT_FORBID_REUSE, 1<span>); </span><span>22</span> curl_setopt(<span>$ch</span>, CURLOPT_TIMEOUT, <span>$timeout</span><span>); </span><span>23</span> <span>if</span> (<span>$refererUrl</span><span>) { </span><span>24</span> curl_setopt(<span>$ch</span>, CURLOPT_REFERER, <span>$refererUrl</span><span>); </span><span>25</span> <span> } </span><span>26</span> <span>if</span>(<span>$contentType</span><span>) { </span><span>27</span> curl_setopt(<span>$ch</span>, CURLOPT_HTTPHEADER, <span>array</span>('Content-Type:'.<span>$contentType</span><span>)); </span><span>28</span> <span> } </span><span>29</span> <span>if</span>(<span>is_string</span>(<span>$data</span><span>)){ </span><span>30</span> curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$data</span><span>); </span><span>31</span> } <span>else</span><span> { </span><span>32</span> curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>http_build_query</span>(<span>$data</span><span>)); </span><span>33</span> <span> } </span><span>34</span> } <span>else</span> <span>if</span>('GET' === <span>strtoupper</span>(<span>$method</span><span>)) { </span><span>35</span> <span>if</span>(<span>is_string</span>(<span>$data</span><span>)) { </span><span>36</span> <span>$real_url</span> = <span>$url</span>. (<span>strpos</span>(<span>$url</span>, '?') === <span>false</span> ? '?' : ''). <span>$data</span><span>; </span><span>37</span> } <span>else</span><span> { </span><span>38</span> <span>$real_url</span> = <span>$url</span>. (<span>strpos</span>(<span>$url</span>, '?') === <span>false</span> ? '?' : ''). <span>http_build_query</span>(<span>$data</span><span>); </span><span>39</span> <span> } </span><span>40</span> <span>41</span> <span>$ch</span> = curl_init(<span>$real_url</span><span>); </span><span>42</span> curl_setopt(<span>$ch</span>, CURLOPT_HEADER, 0<span>); </span><span>43</span> curl_setopt(<span>$ch</span>, CURLOPT_HTTPHEADER, <span>array</span>('Content-Type:'.<span>$contentType</span><span>)); </span><span>44</span> curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, 1<span>); </span><span>45</span> curl_setopt(<span>$ch</span>, CURLOPT_TIMEOUT, <span>$timeout</span><span>); </span><span>46</span> <span>if</span> (<span>$refererUrl</span><span>) { </span><span>47</span> curl_setopt(<span>$ch</span>, CURLOPT_REFERER, <span>$refererUrl</span><span>); </span><span>48</span> <span> } </span><span>49</span> } <span>else</span><span> { </span><span>50</span> <span>$args</span> = <span>func_get_args</span><span>(); </span><span>51</span> <span>return</span> <span>false</span><span>; </span><span>52</span> <span> } </span><span>53</span> <span>54</span> <span>if</span>(<span>$proxy</span><span>) { </span><span>55</span> curl_setopt(<span>$ch</span>, CURLOPT_PROXY, <span>$proxy</span><span>); </span><span>56</span> <span> } </span><span>57</span> <span>$ret</span> = curl_exec(<span>$ch</span><span>); </span><span>58</span> <span>$info</span> = curl_getinfo(<span>$ch</span><span>); </span><span>59</span> <span>$contents</span> = <span>array</span><span>( </span><span>60</span> 'httpInfo' => <span>array</span><span>( </span><span>61</span> 'send' => <span>$data</span>, <span>62</span> 'url' => <span>$url</span>, <span>63</span> 'ret' => <span>$ret</span>, <span>64</span> 'http' => <span>$info</span>, <span>65</span> <span> ) </span><span>66</span> <span> ); </span><span>67</span> <span>68</span> curl_close(<span>$ch</span><span>); </span><span>69</span> <span>return</span> <span>$ret</span><span>; </span><span>70</span> }
调用 WCF接口 的一个例子:$json = restRequest($r_url,'POST', json_encode($data));