RESTfulAPI学习笔记

来源:互联网 发布:sql server链接服务器 编辑:程序博客网 时间:2024/06/14 18:19

总述:

rest是简单的web api,可以利用rest使用http方法向url做出请求(get/post)
get 获取资源
post 增加资源
put 替换资源
delete 删除资源

PHP获取远程url的三种方式

1 标准文件函数

file_get_contents()函数
fopen()
需要打开allow_url_fopen 配置,一般默认都打开了

2 curl 扩展

curl使用的四个步骤:
(1)初始化
    curl_init()
  (2)设置变量
    curl_setopt() 。最为重要,一切玄妙均在此。有一长串cURL参数可供设置,它们能指定URL请求的各个细节。要一次性全部看完并理解可能比较困难,所以今天我们只试一下那些更常用也更有用的选项。
  (3)执行并获取结果
    curl_exec()
  (4)释放cURL句柄
    curl_close()

3 PEAR 的HTTP_Request2类

用get方法获取url

要包含一个查询字符串变量的函数
需要使用http_bulid_query($arr);$arr是键值对
例如:
$var= ['pagesize'=>6,'search'=>'keyword'];$qs = http_build_query($var);$url = 'http://simple.com?'.$qs;file_get_contents($url);
要获取一个受保护的页面,需要提供账号和密码
例:用户名是username,密码是password
$url = 'http://username:password@www.simplae.com/login';
file_get_contents($url);
或者:
$c = curl_init('http://www.simple.com');curl_setopt($c,CURLOPT_RETURNTRANSFER,true);curl_setopt($c,CURLOPT_USERPWD,'username:password');$page = curl_exec($c);curl_close($c);

用post方法获取url

使用http流时使用method和content流上下文选项

$url = 'http://www.simple.com/submit.php';

// 提交的表单数据,编码为查询字符串样式

// 名-值对

$body = ‘monkey=uncle$rhino=aunt’;

$options = array(‘method’=>’post’,

                   ‘content’=>’body’,

                   ‘header’=>’Content-type:application/x-www-form-urlencode’

);

// 创建流上下文

$context  =stream_context_create(array(‘http’=>$options));

// 将上下文传入file_get_contents($url,false,$context);

Print  file_get_contents($url,false,$context);

利用curl时,设置CURLOPT_POST和CURLOPT_POSTFILEDS选项

$url = 'http://www.simple.com/submit.php';

// 提交的表单数据,编码为查询字符串样式

// 名-值对

$body = ‘monkey=uncle$rhino=aunt’;

$c = curl_init($url);

Curl_setopt($c,CURLOPT_POST,true);

Curl_setopt($c,CURL_POSTFILEDS,$body);

Curl_setopt($c,CURLOPT_RETURNTRANSFER,true);

$page = curl_exec($c);

Curl_close($c);

用任意方法和post体获取url

希望使用某个方法请求一个URL,如post,put或delete,post或put请求可能包含格式化数据,如json和xml

 

用http流设置method,header,content流上下文选项:

$url = ‘http://www.example.com/meals/1324’;

$header = ‘Content-Type:application/json’;

$body = ‘[

         {

                   ‘type’:’appetizer’,

                   ‘dish’:’chinkensoup’

},{

                   ‘type’:’appetizer2’,

                   ‘dish’:’chinkensoup2’

}

]’;

$options = array(

         ‘method’=>’put’,

‘header’=>$header,

‘content’=>$body

);

// 创建流上下文

$context  =stream_context_create(array(‘http’=>$options));

// 将上下文传入file_get_contents($url,false,$context);

Print  file_get_contents($url,false,$context);

利用curl时,将CURLOPT_CUSTOMREQUEST选项设置为方法名,如果要包含请求体,将CURLOPT_HTTPHEADER设置为Content-type,另外将CURLOPT_POSTFIELDS设置为请求体

$url = ‘http://www.example.com/meals/1324’;

$body = ‘[

         {

                   ‘type’:’appetizer’,

                   ‘dish’:’chinkensoup’

},{

                   ‘type’:’appetizer2’,

                   ‘dish’:’chinkensoup2’

}

]’;

$c = curl_init($url);

Curl_setopt($c,CURLOPT_CUSTORMREQUEST,’PUT’);

Curl_setopt($c,CURLOPT_HTTPHEADER,array(‘Content-type:application/json’));

Curl_setopt($c,CURLOPT_POSTFIELDS,$body);

Curl_setopt($c,CURLOPT_RETURNTRANSFER,true);

$page = curl_exec($c);

Curl_close($c);

使用curl和put上传文件

$url = ‘http://www.example.com/upload.php’;

$filename = ‘/usr/local/images/pig.jpg’;

$fp = fopen($filename,’r’);

$c = curl_init($url);

Curl_setopt($c,CURLOPT_PUT,true);

Curl_setopt($c,CURLOPT_INFILE,$fp);

Curl_setopt$c,CURLOPT_INFILESIZE,filesize($filename));

Curl_setopt($c,CURLOPT_RETURNTRANSFER,true);

$page = curl_exec($c);

Print $page;

Curl_close($c);

用cookie获取url

希望获取一个页面,要求随页面请求发送一个cookie

使用curl时,可以使用CURLOPOT_COOKIE选项:

$c = curl_init(‘http://www.example.com/needscookie.php’);

Curl_setopt($c,CURLOPT_COOKIE,’user=ellen;activity=swimming’);

Curl_setopt($c,CURLOPT_RETURNTRANSFER,true);

$page = curl_exec($c);