PHP跨站点对接访问请求

网上很多的对接其他网站的接口的代码,每次都需要百度现找,而且不一定合适,找到一份合适的,记录一下

public function fun()
{    
    //进行一些事前的逻辑业务运算

     /*同步*/
     header('Content-type:text/html;charset=utf-8');
                
     //要请求访问的地址
     $url = "";
                         
     $method = "POST";
               
     //拼接要传递的参数
     $params = "order_id=".$order->id."&order_time=".date("Y-m-d H:i:s", time())."&original_merchant_id=".$card_id."&order_sn=".$order->order_no."&merchant_name=".$card['title']."&reality_money=".$origina."&behind_preferrence=".$price."&status=1&coupon=".$order->coupon_discount_price."&phone=".$user['mobile'];
                
                
                
     $header = array();
     $header[] = 'User-Agent: Apipost client Runtime/+https://www.apipost.cn/';
     $header[] = 'Content-Type: application/x-www-form-urlencoded';
                 
                 
     $content = $this->linkcurl($url,$method,$params,$header);
             
     $result = json_decode($content,true);

    //拿到返回接口,然后进行数据处理
}


public function linkcurl($url,$method,$params=false,$header=false)
{
     $httpInfo = array();
     $ch = curl_init();
             
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     curl_setopt($ch, CURLOPT_FAILONERROR, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             
     if (1 == strpos("$".$url, "https://"))
     {
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     }
     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         
     if($method == "POST" ){
         curl_setopt( $ch , CURLOPT_POST , true );
         curl_setopt( $ch , CURLOPT_POSTFIELDS,$params);
     }else if($params){
         curl_setopt( $ch , CURLOPT_URL , $url.'?'.http_build_query($params) );
     }
     $response = curl_exec( $ch );
     if ($response === FALSE) {
          //echo "cURL Error: " . curl_error($ch);
          return false;
     }
     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
     curl_close( $ch );
     return $response;
            
            
            
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值