Rumah > pembangunan bahagian belakang > tutorial php > php发送get、post请求的几种方法,getpost_PHP教程

php发送get、post请求的几种方法,getpost_PHP教程

WBOY
Lepaskan: 2016-07-12 08:51:25
asal
784 orang telah melayarinya

php发送get、post请求的几种方法,getpost

转自:http://blog.csdn.net/haha00217/article/details/7969504

方法1: 用file_get_contents 以get方式获取内容

<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> ?>  
Salin selepas log masuk

方法2: 用fopen打开url, 以get方式获取内容

<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>
Salin selepas log masuk

<span>1</span> <span>stream_get_meta_data</span>(<span>$fp</span>);  
Salin selepas log masuk

<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>  
Salin selepas log masuk

方法3:用file_get_contents函数,以post方式获取url

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

<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> ); 
Salin selepas log masuk

<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> ?>  
Salin selepas log masuk

方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启

<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> ?>  
Salin selepas log masuk

方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和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> ?>  
Salin selepas log masuk

方法6:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

<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> ?>  
Salin selepas log masuk

 

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1130487.htmlTechArticlephp发送get、post请求的几种方法,getpost 转自:http://blog.csdn.net/haha00217/article/details/7969504 方法1: 用file_get_contents 以get方式获取内容 1 ? php 2...
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan