/ Published in: PHP
A set of drop in rest methods for php. Curl options can be added/removed/modified, but this gets you going.
Various sources were used to build this code snippet - I can't remember all of them. At least one of the sources was http://snipplr.com/view/17733/php-curl-post/
Expand |
Embed | Plain Text
function construct_query_string($data_array) { foreach($data_array as $key => $value) { } } function http_get($url) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_HTTPGET, true); // SSL verifier should be set to true for higher // security, but causes issues on some platforms curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($c); curl_close($c); return $output; } // $data_string should be in the format of something like "name=fred&age=12" // use the "construct_query_string" method above to build this string from an array function http_post($url, $data_string) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_POST, 1); // SSL verifier should be set to true for higher // security, but causes issues on some platforms curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($c, CURLOPT_POSTFIELDS, $data_string); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($c); curl_close($c); return $output; } function http_delete($url) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); // SSL verifier should be set to true for higher // security, but causes issues on some platforms curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($c); curl_close($c); return $output; }
You need to login to post a comment.
