Skip to content

Instantly share code, notes, and snippets.

@codcodog
Created November 12, 2019 06:18
Show Gist options
  • Save codcodog/229604b438ff9e51545c564888b1f172 to your computer and use it in GitHub Desktop.
Save codcodog/229604b438ff9e51545c564888b1f172 to your computer and use it in GitHub Desktop.
curl 实现的 get/post 请求样例
<?php
/**
* @Author Cryven
* @Date 2019-11-12 13:40:42
*/
function get($url, $params, $timeout = 3)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
function post($url, $params, $timeout = 3)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment