Home >Backend Development >PHP Tutorial >Several methods for PHP to send get and post requests, getpost_PHP tutorial

Several methods for PHP to send get and post requests, getpost_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:51:25802browse

Several ways for PHP to send get and post requests, getpost

Reprinted from: http://blog.csdn.net/haha00217/article/details/7969504

Method 1: Use file_get_contents to get the content in get mode

<span>1</span> <?<span>php  
</span><span>2</span> <span>$url</span>='http://www.domain.com/'<span>;  
</span><span>3</span> <span>$html</span> = <span>file_get_contents</span>(<span>$url</span><span>);  
</span><span>4</span> <span>echo</span> <span>$html</span><span>;  
</span><span>5</span> ?>  

Method 2: Use fopen to open the url and get the content using get method

<span>1</span> <?<span>php  
</span><span>2</span> <span>$fp</span> = <span>fopen</span>(<span>$url</span>, 'r'<span>);  
</span><span>3</span> <span>//</span><span>返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等) </span>

<span>1</span> <span>stream_get_meta_data</span>(<span>$fp</span>);  

<span>1</span> <span>while</span>(!<span>feof</span>(<span>$fp</span><span>)) {  
</span><span>2</span> <span>$result</span> .= <span>fgets</span>(<span>$fp</span>, 1024<span>);  
</span><span>3</span> <span>}  
</span><span>4</span> <span>echo</span> "url body: <span>$result</span>"<span>;  
</span><span>5</span> <span>fclose</span>(<span>$fp</span><span>);  
</span><span>6</span> ?>  
<span>7</span>  

Method 3: Use the file_get_contents function to get the url in post mode

<span>1</span> <?<span>php  
</span><span>2</span> <span>$data</span> = <span>array</span> ('foo' => 'bar');  

<span> 1</span> <span>//</span><span>生成url-encode后的请求字符串,将数组转换为字符串  </span>
<span> 2</span> <span>$data</span> = <span>http_build_query</span>(<span>$data</span><span>);  
</span><span> 3</span> <span>$opts</span> = <span>array</span><span> (  
</span><span> 4</span> <span>array</span><span> (  
</span><span> 5</span> <span> 6</span> <span> 7</span> <span>strlen</span>(<span>$data</span>) . "\r\n",  
<span> 8</span> <span>$data</span>  
<span> 9</span> <span>)  
</span><span>10</span> ); 

<span>1</span> <span>//</span><span>生成请求的句柄文件  </span>
<span>2</span> <span>$context</span> = <span>stream_context_create</span>(<span>$opts</span><span>);  
</span><span>3</span> <span>$html</span> = <span>file_get_contents</span>('http://localhost/e/admin/test.html', <span>false</span>, <span>$context</span><span>);  
</span><span>4</span> <span>echo</span> <span>$html</span><span>;  
</span><span>5</span> ?>  

Method 4: Use the fsockopen function to open the url and obtain the complete data in get mode, including header and body. fsockopen requires the allow_url_fopen option in PHP.ini to be turned on

<span> 1</span> <?<span>php  
</span><span> 2</span> <span>function</span> get_url (<span>$url</span>,<span>$cookie</span>=<span>false</span><span>)  
</span><span> 3</span> <span>{  
</span><span> 4</span> <span>$url</span> = <span>parse_url</span>(<span>$url</span><span>);  
</span><span> 5</span> <span>$query</span> = <span>$url</span>[path]."?".<span>$url</span><span>[query];  
</span><span> 6</span> <span>echo</span> "Query:".<span>$query</span><span>;  
</span><span> 7</span> <span>$fp</span> = <span>fsockopen</span>( <span>$url</span>[host], <span>$url</span>[port]?<span>$url</span>[port]:80 , <span>$errno</span>, <span>$errstr</span>, 30<span>);  
</span><span> 8</span> <span>if</span> (!<span>$fp</span><span>) {  
</span><span> 9</span> <span>return</span> <span>false</span><span>;  
</span><span>10</span> } <span>else</span><span> {  
</span><span>11</span> <span>$request</span> = "GET <span>$query</span> HTTP/1.1\r\n"<span>;  
</span><span>12</span> <span>$request</span> .= "Host: <span>$url</span>[host]\r\n"<span>;  
</span><span>13</span> <span>$request</span> .= "Connection: Close\r\n"<span>;  
</span><span>14</span> <span>if</span>(<span>$cookie</span>) <span>$request</span>.="Cookie:   <span>$cookie</span>\n"<span>;  
</span><span>15</span> <span>$request</span>.="\r\n"<span>;  
</span><span>16</span> <span>fwrite</span>(<span>$fp</span>,<span>$request</span><span>);  
</span><span>17</span> <span>while</span><span>()) {  
</span><span>18</span> <span>$result</span> .= @<span>fgets</span>(<span>$fp</span>, 1024<span>);  
</span><span>19</span> <span>}  
</span><span>20</span> <span>fclose</span>(<span>$fp</span><span>);  
</span><span>21</span> <span>return</span> <span>$result</span><span>;  
</span><span>22</span> <span>}  
</span><span>23</span> <span>}  
</span><span>24</span> <span>//</span><span>获取url的html部分,去掉header  </span>
<span>25</span> <span>function</span> GetUrlHTML(<span>$url</span>,<span>$cookie</span>=<span>false</span><span>)  
</span><span>26</span> <span>{  
</span><span>27</span> <span>$rowdata</span> = get_url(<span>$url</span>,<span>$cookie</span><span>);  
</span><span>28</span> <span>if</span>(<span>$rowdata</span><span>)  
</span><span>29</span> <span>{  
</span><span>30</span> <span>$body</span>= <span>stristr</span>(<span>$rowdata</span>,"\r\n\r\n"<span>);  
</span><span>31</span> <span>$body</span>=<span>substr</span>(<span>$body</span>,4,<span>strlen</span>(<span>$body</span><span>));  
</span><span>32</span> <span>return</span> <span>$body</span><span>;  
</span><span>33</span> <span>}  
</span><span>34</span>     <span>return</span> <span>false</span><span>;  
</span><span>35</span> <span>}  
</span><span>36</span> ?>  

Method 5: Use the fsockopen function to open the url and obtain the complete data in POST mode, including header and body

<span> 1</span> <?<span>php  
</span><span> 2</span> <span>function</span> HTTP_Post(<span>$URL</span>,<span>$data</span>,<span>$cookie</span>, <span>$referrer</span>=""<span>)  
</span><span> 3</span> <span>{  
</span><span> 4</span>     <span>//</span><span> parsing the given URL  </span>
<span> 5</span> <span>$URL_Info</span>=<span>parse_url</span>(<span>$URL</span><span>);  
</span><span> 6</span>     <span>//</span><span> Building referrer  </span>
<span> 7</span> <span>if</span>(<span>$referrer</span>=="") <span>//</span><span> if not given use this script as referrer  </span>
<span> 8</span> <span>$referrer</span>="111"<span>;  
</span><span> 9</span>     <span>//</span><span> making string from $data  </span>
<span>10</span> <span>foreach</span>(<span>$data</span> <span>as</span> <span>$key</span>=><span>$value</span><span>)  
</span><span>11</span> <span>$values</span>[]="<span>$key</span>=".<span>urlencode</span>(<span>$value</span><span>);  
</span><span>12</span> <span>$data_string</span>=<span>implode</span>("&",<span>$values</span><span>);  
</span><span>13</span>     <span>//</span><span> Find out which port is needed - if not given use standard (=80)  </span>
<span>14</span> <span>if</span>(!<span>isset</span>(<span>$URL_Info</span>["port"<span>]))  
</span><span>15</span> <span>$URL_Info</span>["port"]=80<span>;  
</span><span>16</span>     <span>//</span><span> building POST-request:  </span>
<span>17</span> <span>$request</span>.="POST ".<span>$URL_Info</span>["path"]." HTTP/1.1\n"<span>;  
</span><span>18</span> <span>$request</span>.="Host: ".<span>$URL_Info</span>["host"]."\n"<span>;  
</span><span>19</span> <span>$request</span>.="Referer: <span>$referer</span>\n"<span>;  
</span><span>20</span> <span>$request</span>.="Content-type: application/x-www-form-urlencoded\n"<span>;  
</span><span>21</span> <span>$request</span>.="Content-length: ".<span>strlen</span>(<span>$data_string</span>)."\n"<span>;  
</span><span>22</span> <span>$request</span>.="Connection: close\n"<span>;  
</span><span>23</span>     <span>$request</span>.="Cookie:   <span>$cookie</span>\n"<span>;  
</span><span>24</span>     <span>$request</span>.="\n"<span>;  
</span><span>25</span> <span>$request</span>.=<span>$data_string</span>."\n"<span>;  
</span><span>26</span>     <span>$fp</span> = <span>fsockopen</span>(<span>$URL_Info</span>["host"],<span>$URL_Info</span>["port"<span>]);  
</span><span>27</span> <span>fputs</span>(<span>$fp</span>, <span>$request</span><span>);  
</span><span>28</span> <span>while</span>(!<span>feof</span>(<span>$fp</span><span>)) {  
</span><span>29</span> <span>$result</span> .= <span>fgets</span>(<span>$fp</span>, 1024<span>);  
</span><span>30</span> <span>}  
</span><span>31</span> <span>fclose</span>(<span>$fp</span><span>);  
</span><span>32</span>     <span>return</span> <span>$result</span><span>;  
</span><span>33</span> <span>}  
</span><span>34</span> ?>  

Method 6: Use the curl library. Before using the curl library, you may need to check whether the curl extension has been turned on in php.ini

<span> 1</span> <?<span>php  
</span><span> 2</span> <span>$ch</span> =<span> curl_init();  
</span><span> 3</span> <span>$timeout</span> = 5<span>;  
</span><span> 4</span> curl_setopt (<span>$ch</span>, CURLOPT_URL, 'http://www.domain.com/'<span>);  
</span><span> 5</span> curl_setopt (<span>$ch</span>, CURLOPT_RETURNTRANSFER, 1<span>);  
</span><span> 6</span> curl_setopt (<span>$ch</span>, CURLOPT_CONNECTTIMEOUT, <span>$timeout</span><span>);  
</span><span> 7</span> <span>$file_contents</span> = curl_exec(<span>$ch</span><span>);  
</span><span> 8</span> curl_close(<span>$ch</span><span>);  
</span><span> 9</span> <span>echo</span> <span>$file_contents</span><span>;  
</span><span>10</span> ?>  

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1130487.htmlTechArticleSeveral methods of sending get and post requests in php, getpost is transferred from: http://blog.csdn.net/ haha00217/article/details/7969504 Method 1: Use file_get_contents to get content 1 in get mode? php 2...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn