Created
February 12, 2018 08:29
-
-
Save MaraScott/1fa763ac36b4ea3bbc51098b7d1cfe3a to your computer and use it in GitHub Desktop.
Curl php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // namespace App\Http\Custom; | |
| class Curl | |
| { | |
| public static function get($url){ | |
| $ch = curl_init($url); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $result = curl_exec($ch); | |
| return $result; | |
| } | |
| public static function post($url, $params = []){ | |
| $post_data = ''; | |
| foreach($params as $k => $v){ | |
| $post_data .= $k . '='.$v.'&'; | |
| } | |
| rtrim($post_data, '&'); | |
| $ch = curl_init(); | |
| curl_setopt($ch,CURLOPT_URL,$url); | |
| curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); | |
| curl_setopt($ch,CURLOPT_HEADER, false); | |
| curl_setopt($ch, CURLOPT_POST, count($post_data)); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
| $output = curl_exec($ch); | |
| curl_close($ch); | |
| return $output; | |
| } | |
| public static function put($url, $params = []){ | |
| $post_data = ''; | |
| foreach($params as $k => $v){ | |
| $post_data .= $k . '='.$v.'&'; | |
| } | |
| rtrim($post_data, '&'); | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
| curl_setopt($ch,CURLOPT_URL,$url); | |
| curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); | |
| curl_setopt($ch,CURLOPT_HEADER, false); | |
| curl_setopt($ch, CURLOPT_POST, count($post_data)); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
| $output = curl_exec($ch); | |
| curl_close($ch); | |
| return $output; | |
| } | |
| public static function delete($url, $params = []){ | |
| $post_data = ''; | |
| foreach($params as $k => $v){ | |
| $post_data .= $k . '='.$v.'&'; | |
| } | |
| rtrim($post_data, '&'); | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
| curl_setopt($ch,CURLOPT_URL,$url); | |
| curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); | |
| curl_setopt($ch,CURLOPT_HEADER, false); | |
| curl_setopt($ch, CURLOPT_POST, count($post_data)); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
| $output = curl_exec($ch); | |
| curl_close($ch); | |
| return $output; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment