封装 GET 和POST方式请求远程API 的方法

拥有回忆 提交于 2019-12-03 23:58:37

get 方式

function http_get_content($url){    $oCurl = curl_init();    if (stripos($url, "http://") !== FALSE) {        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);//是否验证证书,        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);//是否验证主机        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1    }    curl_setopt($oCurl, CURLOPT_URL, $url);    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); //成功返回结果,否则返回false    $sContent = curl_exec($oCurl);    $aStatus = curl_getinfo($oCurl);    curl_close($oCurl);    if (intval($aStatus["http_code"]) == 200) {        return $sContent;    } else {        return false;    }}调用实例
function checkLogisticsByExpressNumber($expressNumber){    $url = 'https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?cb=jQuery110204759692032715892_1499865778178&appid=4001&com=&nu='.$expressNumber;    $result = http_get_content($url);    return $result;}
post 方式
function http_post_content($url, $param, $post_file = false){    $oCurl = curl_init();    if (stripos($url, "http://") !== FALSE) {        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1    }    if (is_string($param) || $post_file) {        $strPOST = $param;    } else {        $aPOST = array();        foreach ($param as $key => $val) {            $aPOST[] = $key . "=" . urlencode($val);        }        $strPOST = join("&", $aPOST);    }    curl_setopt($oCurl, CURLOPT_URL, $url);    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($oCurl, CURLOPT_POST, true);    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);    $sContent = curl_exec($oCurl);    $aStatus = curl_getinfo($oCurl);    curl_close($oCurl);    if (intval($aStatus["http_code"]) == 200) {        return $sContent;    } else {        return false;    }}调用实例
function BusinesswxSendAppMessage($access_token,$touser,$msgtype,$agentid,$content_type,$type,$url,$submit_record_id,$text_template,$nickname,$form_name=null,$customer_name=null){    $text_template = str_replace('<br/>', '\n', $text_template);    $touser = (array)$touser;    //array_push($touser,'QiaoChaoYu');    $user = implode('|',$touser);    if(!$form_name){        $form_name = '客户沟通';    }    if($content_type == 1){        if($form_name == '客户沟通'){            $title = '【客户'.$customer_name.'】';            $description = $nickname  . '发送了图片消息';        }else{            $title = '【'.$form_name.'】';            $description = $nickname . '发送了图片消息';        }    }elseif($content_type == 2){        if($form_name == '客户沟通'){            $title = '【客户'.$customer_name.'】';            $description =$nickname  . '发送了视频消息';        }else{            $title = '【'.$form_name.'】';            $description =$nickname . '发送了视频消息';        }    }elseif($content_type == 4){        $title = '【拉人入群】';        $description = '【' . $form_name . '】' . '\n'  . $customer_name . '\n\n'.            ''.$text_template.'\n';    }elseif($content_type == 5){        $title = '【审核提醒】';        $description = '【' . $form_name .'】' . '\n' . '提单人: ' . $customer_name . '\n' . '业务编号: ' . $submit_record_id . '\n\n';    }elseif($content_type == 6){//提交表单        $title = '【'.$form_name.'】';        $description = '[提单人]: ' . $customer_name . '\n\n'.            ''.$text_template.'\n';    }elseif($content_type == 7){        $title = '【表单修改提醒】';        $description = '【' . $form_name .'】' . '\n' . '提单人: ' . $customer_name . '\n\n'.            ''.$text_template.'\n';    }elseif($content_type == 8){        $title = '【同意提醒】';        $description = '【' . $form_name .'】' . '\n' . '提单人: ' . $customer_name . '\n\n'.            ''.$text_template.'\n';    }elseif(100 == $content_type){//是删除信息        //删除信息, 不发提醒        exit;    }else{        if($type == 'salesmanMessage'){            $title = '【' . $form_name .'】';            $description =   $customer_name . '\n' ."发送人:". $nickname . "\n\n" . $text_template.'\n';        }else if($type == 'customerMessage'){            $title = '【' . $form_name .'】';            $description =  $customer_name . '\n' ."发送人:". $nickname . "\n\n" .$text_template.'\n';        }    }    $dataJson = '{            "touser":"' . $user . '",            "msgtype":"' . $msgtype . '",            "agentid":"' . $agentid . '",            "textcard": {               "title" :"'. $title.'",               "description" :"'. $description.'",               "url" :"'. $url.'",               "btntxt":"点击查看详情"            }    }';    $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$access_token;    $result = http_post_content($url,$dataJson);    return $result;}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!