我从来没有看到如何PUT/DELETE
发送请求.
如何用PHP做到这一点?
我知道如何使用curl发送GET/POST请求:
$ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 4);
但是如何做PUT
/ DELETE
要求?
对于DELETE
使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
对于PUT
使用curl_setopt($ch, CURLOPT_PUT, true);
不依赖于安装卷曲另一种方法是使用file_get_contents
一个自定义的HTTP流上下文.
$result = file_get_contents( 'http://example.com/submit.php', false, stream_context_create(array( 'http' => array( 'method' => 'DELETE' ) )) );
查看这两篇关于使用PHP进行REST的文章
http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
http://www.gen-x-design.com/archives/making-restful-requests-in-php/