您的位置 首页 php

php curl发送请求详细教程

如果你想了解更多关于php的知识,可以点击:php教程

  cURL可以使用URL的语法模拟浏览器来传输数据,因为它是模拟浏览器,因此它同样支持多种协议,FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP等协议都可以很好的支持,包括一些:HTTPS认证,HTTP POST方法,HTTP PUT方法,FTP上传,keyberos认证,HTTP上传,代理服务器,cookies,用户名/密码认证,下载文件断点续传,上传文件断点续传,http代理服务器管道,甚至它还支持IPv6,scoket5代理服务器,通过http代理服务器上传文件到FTP服务器等等。

  使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了。无论是你想从从一个链接上取部分数据,或是取一个XML文件并把其导入数据库,那怕就是简单的获取网页内容,cURL 是一个功能强大的PHP库。

1、PHP建立CURL请求的基本步骤

①:初始化

  curl_init()

②:设置属性

  curl_setopt().有一长串cURL参数可供设置,它们能指定URL请求的各个细节。

③:执行并获取结果

  curl_exec()

④:释放句柄

  curl_close()

2、实例:

php curl 发送get请求:

//初始化$curl = curl_init();//设置抓取的urlcurl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');//设置头文件的信息作为数据流输出curl_setopt($curl, CURLOPT_HEADER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//执行命令$data = curl_exec($curl);echo curl_getinfo($curl,CURLINFO_HTTP_CODE); //输出请求状态码//关闭URL请求curl_close($curl);//显示获得的数据print_r($data);

php curl 发送post请求:

//初始化$curl = curl_init();//设置抓取的urlcurl_setopt($curl, CURLOPT_URL, 'http://localhost/test/test.php');//设置头文件的信息作为数据流输出curl_setopt($curl, CURLOPT_HEADER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置post方式提交curl_setopt($curl, CURLOPT_POST, 1);//设置post数据$post_data = array(    "title" => "1290800466",    "content" => "3424243243");//post提交的数据curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//执行命令$data = curl_exec($curl);//关闭URL请求curl_close($curl);//显示获得的数据print_r($data);

test.php:

<?php$title = $_REQUEST['title'];$content = $_REQUEST['content'];error_log($title."\n",3,'error.log');error_log($content."\n",3,'error.log');echo 'ok';

3、PHP cURL 函数:

以下包含了PHP cURL函数列表:

函数描述
curl_close()关闭一个cURL会话。
curl_copy_handle()复制一个cURL句柄和它的所有选项。
curl_errno()返回最后一次的错误号。
curl_error()返回一个保护当前会话最近一次错误的字符串。
curl_escape()返回转义字符串,对给定的字符串进行URL编码。
curl_exec()执行一个cURL会话。
curl_file_create()创建一个 CURLFile 对象。
curl_getinfo()获取一个cURL连接资源句柄的信息。
curl_init()初始化一个cURL会话。
curl_multi_add_handle()向curl批处理会话中添加单独的curl句柄。
curl_multi_close()关闭一组cURL句柄。
curl_multi_exec()运行当前 cURL 句柄的子连接。
curl_multi_getcontent()如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流。
curl_multi_info_read()获取当前解析的cURL的相关传输信息。
curl_multi_init()返回一个新cURL批处理句柄。
curl_multi_remove_handle()移除curl批处理句柄资源中的某个句柄资源。
curl_multi_select()等待所有cURL批处理中的活动连接。
curl_multi_setopt()设置一个批处理cURL传输选项。
curl_multi_strerror()返回描述错误码的字符串文本。
curl_pause()暂停及恢复连接。
curl_reset()重置libcurl的会话句柄的所有选项。
curl_setopt_array()为cURL传输会话批量设置选项。
curl_setopt()设置一个cURL传输选项。
curl_share_close()关闭cURL共享句柄。
curl_share_init()初始化cURL共享句柄。
curl_share_setopt()设置一个共享句柄的cURL传输选项。
curl_strerror()返回错误代码的字符串描述。
curl_unescape()解码URL编码后的字符串。
curl_version()获取cURL版本信息。

成功时返回 TRUE, 或者在失败时返回 FALSE。

一些状态代码

1xx:请求收到,继续处理

2xx:操作成功收到,分析、接受

3xx:完成此请求必须进一步处理

4xx:请求包含一个错误语法或不能完成

5xx:服务器执行一个完全有效请求失败

100——客户必须继续发出请求

101——客户要求服务器根据请求转换HTTP协议版本

200——交易成功

201——提示知道新文件的URL

202——接受和处理、但处理未完成

203——返回信息不确定或不完整

204——请求收到,但返回信息为空

205——服务器完成了请求,用户代理必须复位当前已经浏览过的文件

206——服务器已经完成了部分用户的GET请求

300——请求的资源可在多处得到

301——删除请求数据

302——在其他地址发现了请求数据

303——建议客户访问其他URL或访问方式

304——客户端已经执行了GET,但文件未变化

305——请求的资源必须从服务器指定的地址得到

306——前一版本HTTP中使用的代码,现行版本中不再使用

307——申明请求的资源临时性删除

400——错误请求,如语法错误

401——请求授权失败

402——保留有效ChargeTo头响应

403——请求不允许

404——没有发现文件、查询或URl

405——用户在Request-Line字段定义的方法不允许

406——根据用户发送的Accept拖,请求资源不可访问

407——类似401,用户必须首先在代理服务器上得到授权

408——客户端没有在用户指定的饿时间内完成请求

409——对当前资源状态,请求不能完成

410——服务器上不再有此资源且无进一步的参考地址

411——服务器拒绝用户定义的Content-Length属性请求

412——一个或多个请求头字段在当前请求中错误

413——请求的资源大于服务器允许的大小

414——请求的资源URL长于服务器允许的长度

415——请求资源不支持请求项目格式

416——请求中包含Range请求头字段,在当前请求资源范围内没有range指示值,请求

也不包含If-Range请求头字段

417——服务器不满足请求Expect头字段指定的期望值,如果是代理服务器,可能是下一级服务器不能满足请求

500——服务器产生内部错误

501——服务器不支持请求的函数

502——服务器暂时不可用,有时是为了防止发生系统过载

503——服务器过载或暂停维修

504——关口过载,服务器使用另一个关口或服务来响应用户,等待时间设定值较长

505——服务器不支持或拒绝支请求头中指定的HTTP版本

以上就是php curl发送请求详细教程的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:php curl发送请求详细教程

文章地址:https://www.zhihuclub.com/3856.shtml

关于作者: 智云科技

热门文章

评论已关闭

30条评论

  1. I really enjoy the blog article. Really looking forward to read more. Want more. Trudi Mitchell Aalst

  2. Pretty! This was an incredibly wonderful article. Thank you for supplying these details. Meg Tye Crowell

  3. Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the site is also really good. Natalya Elmer Daphne

  4. I am regular visitor, how are you everybody? This piece of writing posted at this site is in fact nice. Bab Creight Gorrian

  5. Ahaa, its nice dialogue concerning this paragraph here at this blog, I have read all that, so now me also commenting here. Darsey Christian McCallion

  6. Hi there! I just want to offer you a huge thumbs up for your great info you have here on this post. I am returning to your site for more soon. Lynett Graig Dino

  7. I visited many blogs but the audio quality for audio songs current at this web page is genuinely superb. Julina Jarad Reuben

  8. Check below, are some totally unrelated internet sites to ours, nonetheless, they may be most trustworthy sources that we use. Rivkah Clement Milton

  9. Way cool! Some extremely valid points! I appreciate you penning this post plus the rest of the site is really good. Ardyth Muffin Simone

  10. Hi there, this weekend is fastidious designed for me, as this point in time i am reading this impressive informative post here at my residence. Anthia Briant Gittle

  11. 523723 49355Ich kenne einige Leute, die aus Kanadakommen. Eines Tages werde ich auch dorthin reisen Lg Daniela 509842 Tina Waverly Bosch

  12. Definitely, what a fantastic site and informative posts, I definitely will bookmark your blog. All the Best! Lesya Roi Chara

  13. If you desire to increase your knowledge just keep visiting this website and be updated with the most recent information posted here. Caren Nathan Sundstrom

  14. A motivating discussion is definitely worth comment. I do believe that you ought to write more about this subject matter, it might not be a taboo matter but typically people do not talk about such topics. To the next! Kind regards! Lorene Dall Tiebold

  15. Meh synth Schlitz, tempor duis single-origin coffee ea next level ethnic fingerstache fanny pack nostrud. Photo booth anim 8-bit hella, PBR 3 wolf moon beard Helvetica. Salvia esse nihil, flexitarian Truffaut synth art party deep v chillwave. Seitan High Life reprehenderit consectetur cupidatat kogi. Deva Renaldo Orlosky

  16. obviously like your website however you have to test the spelling on several of your posts. A number of them are rife with spelling problems and I find it very troublesome to tell the reality however I will definitely come back again. Lara Dewey Freddie

  17. Some really nice and utilitarian info on this site, too I conceive the style has excellent features. Carlotta Garwin Stuart

  18. Simple & Powerful Uploadea is Amazingly easy to Use where to Store Your Data & Save Your Countless Time Faye Cristiano Peers

  19. There may be noticeably a bundle to learn about this. I assume you made certain nice factors in features also. Annice Eliot Annamaria

  20. You made some good points there. I did a search on the topic and found most folks will agree with your blog. Kyla Jamesy Tisbee Orella Rafi Ignacia

  21. They relieve pain and inammation as do traditional NSAIDs but produce fewer gastrointestinal side effects than NSAIDs. – comprare cialis online Tqpiae que es viagra soft Rptrve Mail Order Pharmacy Ayfqgd Cialis E Levitra Forum – cialis prices Buy Semisynthetic Tetracycline Dplhoe

网站地图