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> ?>
方法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>
<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>
方法3:用file_get_contents函数,以post方式获取url
<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> ?>
方法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> ?>
方法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> ?>
方法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> ?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
