laravel curl post json

<?php
namespace App\BO;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use \Curl\Curl;
class Http
{
public function __construct()
{
}
public function post($url, $params) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$post_result = curl_exec($ch);
curl_close($ch);
return $post_result;
}
public function postJson($url, $data_string) {
$ch = curl_init();
//$data_string = http_build_query($arr);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//curl_setopt($curl, CURLOPT_TIMEOUT, 60000);
//curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_PROXY, "127.0.0.1");
//curl_setopt($curl, CURLOPT_PROXYPORT, 8999);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
$post_result = curl_exec($ch);
if (curl_errno($ch)) {
//print curl_error($ch);
}
curl_close($ch);
return $post_result;
}
public function get($url) {
try {
if (!isset($url))
return -1;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
 
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
Log::info('url:'.$url);
$result = curl_exec($curl);
curl_close($curl);
//print_r($result);
return $result;
}
catch (\Exception $ex) {
Log::info($ex);
return -1;
}
}
}
上一篇:CentOs6.8安装Git并安装oh my zsh


下一篇:BZOJ4723[POI2017]Flappy Bird——模拟