load() Function for PHP - Fetch URL Content
来源:互联网 发布:linux怎么启动jenkins 编辑:程序博客网 时间:2024/04/29 03:34
I recently had to develop a small script that willfetch an XML file from the web. All I had to do is download a given URLand read its contents. To my great surprise I found that download thefile using my jx Ajax library was much easier than doing it with PHP.
PHP make this very easy by including functions likefile_get_contents() that has URL support. This code will get you thecontents of an URL.
$contents = file_get_contents('http://example.com/rss.xml');
Unfortunately, this is a huge security threat - and many servershave disabled this feature in PHP. Also this is not the most optimizedmethod to fetch an URL. Also, it is impossible to submit data using thePOST method using this function.
Other Options - curl and fsockopen
PHP provide other two method to fetch an URL - Curl and Fsockopen. But to use this I have to write a lot more code.
load()
So I decided to create my own function that makes it much more easier.
Features
- Easy to use.
- Supports Get and Post methods.
- Supports HTTP Basic Authentication - this will work - http://binny:password@example.com/
- Supports both Curl and Fsockopen. Tries to use curl - if it is not available, users fsockopen.
- Secure URL(https) supported with Curl
Options
The first argument of this function is the URL to be fetched. Thesecond argument is an associative array. This is an optional argument.The following values are supported in this array.
- return_info
- Possible values - true/false
If this is true, the function will return an associative array rather than just a string. The array will contain 3 elements...- headers
- An associative array containing all the headers returned by the server.
- body
- A string - the contents of the URL.
- info
- Some information about the fetch. This is the result returned by the 'curl_getinfo()' function. Supported only with Curl.
- method
- Possible Values - post/get
Specifies the method to be used. - modified_since
- If this option is set, the 'If-Modified-Since' header will be used.This will make sure that the URL will be fetched only it was modified.
Examples
The code to fetch the contents of an URL will look like this...
$contents = load('http://example.com/rss.xml');
Simple, no? This will just return the contents of the URL. If youneed to do more complex stuff, just use the second argument to passmore options...
The output will be like this...
Code
- load() Function for PHP - Fetch URL Content
- AJAX - $().load(url,data,function(response,status,xhr))
- function writelog(filename,content)
- PHP中的CURL函数库(Client URL Library Function)
- PHP中的CURL函数库(Client URL Library Function)
- PHP中的CURL函数库(Client URL Library Function)
- ecplise for php(3.6)错误unsupported content type in editor
- [解决问题]eclipse for php 打开项目出现invalid project content
- Dompdf for PHP & Replace Url to link
- hibernate get/load 懒加载 fetch
- Pwning PHP mail() function For Fun And RCE
- cannot fetch plan for SQL_ID
- chromium load url
- 对比一下$(function () {});和$(window).load(function(){});
- $(window).on("load",function(){}和$(function(){})区别
- $(document).ready(function(){ ... })与$(window).load(function() { }
- $(window).on("load",function(){}和$(function(){})区别
- php function
- 软件测试工程师的进阶要求
- velocity用法简单实例说明
- sip简介
- 推广的海盗分金币问题的算法
- 在vs2008/vs2005中对C++项目做单元测试(Unit Test)
- load() Function for PHP - Fetch URL Content
- 移动MM商城,不可重蹈梦网覆辙
- 莫为SOA而SOA
- SQLServer中的Scanf和Printf
- 修复ubuntu启动项之重写Grub和MBR
- 我的第一个FFT核,成功了
- 性能强劲的Tokyo Cabinet 和 Tokyo Tyrant
- 图文解读Facebook 从设计原则到架构体系
- Facebook - 150亿张照片海量存贮架构