php curl post提交 json数据

function http($url, $data = NULL, $json = true)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
    if($json && is_array($data)){
    $data = json_encode( $data );
    }
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    if($json){ //发送JSON数据
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
    array(
    'Content-Type: application/json; charset=utf-8',
    'Content-Length:' . strlen($data))
    );
    }
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($curl);
    $errorno = curl_errno($curl);

    if ($errorno) {
    return array('errorno' => false, 'errmsg' => $errorno);
    }
    curl_close($curl);
    return json_decode($res, true);
}

2017-10-25_59f0721560a5b.png

雷亮博客
请先登陆后发表评论
  • 最新评论
  • 总共0条评论
  • 本站使用thinkphp搭建 © 2014-2016 blog.wo97.com 版权所有 ICP证:蜀ICP备16024789号
  • 工信部
  • 联系邮箱:1031041088@qq.com