curl以json形式post请求

/**
 * 发送 http 请求
 *
 * @param string $uri 请求的地址
 * @param array $data 发送的数据
 *
 * @return bool
 */

function post_data($url = '', $data = array())
{
    $data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($data) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    } else {
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

请求方式

 		$post_data['device_id'] = 25534;
        $post_data['price'] = '521';
        $post_data['price_type'] ='wechat';
        $post_data['sid'] = 111;
        $push_uri = 'https://www.xxxx.com/ms/xxnpush/dopush';
        $data = post_data($push_uri,$post_data);
        dd($data);

你可能感兴趣的:(php)