1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <?php
class GetData { public $url = 'http://127.0.0.1:1000/data.php';
public function test_curl_get() { $result = $this->curl_get($this->url); var_dump(json_decode($result,true)); }
public function test_curl_post() { $result = $this->curl_post($this->url); var_dump(json_decode($result,true)); }
public function curl_post($url, array $post = array(), array $options = array()) { $defaults = array( CURLOPT_POST => 1, CURLOPT_HEADER => 0, CURLOPT_URL => $url, CURLOPT_FRESH_CONNECT => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FORBID_REUSE => 1, CURLOPT_POSTFIELDS => http_build_query($post), CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0 );
$ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); if (! $result = curl_exec($ch)) { trigger_error(curl_error($ch)); } curl_close($ch); return $result; }
public function curl_get($url, array $get = array(), array $options = array()) { $defaults = array( CURLOPT_URL => $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($get), CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0 ); $ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); if (! $result = curl_exec($ch)) { trigger_error(curl_error($ch)); } curl_close($ch); return $result; } }
$GetData = new GetData; $GetData->test_curl_get(); $GetData->test_curl_post();
|