• PHP跨站点对接访问请求


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

    1. public function fun()
    2. {
    3. //进行一些事前的逻辑业务运算
    4. /*同步*/
    5. header('Content-type:text/html;charset=utf-8');
    6. //要请求访问的地址
    7. $url = "";
    8. $method = "POST";
    9. //拼接要传递的参数
    10. $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'];
    11. $header = array();
    12. $header[] = 'User-Agent: Apipost client Runtime/+https://www.apipost.cn/';
    13. $header[] = 'Content-Type: application/x-www-form-urlencoded';
    14. $content = $this->linkcurl($url,$method,$params,$header);
    15. $result = json_decode($content,true);
    16. //拿到返回接口,然后进行数据处理
    17. }
    18. public function linkcurl($url,$method,$params=false,$header=false)
    19. {
    20. $httpInfo = array();
    21. $ch = curl_init();
    22. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    23. curl_setopt($ch, CURLOPT_URL, $url);
    24. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    25. curl_setopt($ch, CURLOPT_FAILONERROR, false);
    26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    27. if (1 == strpos("$".$url, "https://"))
    28. {
    29. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    30. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    31. }
    32. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    33. curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    34. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    35. if($method == "POST" ){
    36. curl_setopt( $ch , CURLOPT_POST , true );
    37. curl_setopt( $ch , CURLOPT_POSTFIELDS,$params);
    38. }else if($params){
    39. curl_setopt( $ch , CURLOPT_URL , $url.'?'.http_build_query($params) );
    40. }
    41. $response = curl_exec( $ch );
    42. if ($response === FALSE) {
    43. //echo "cURL Error: " . curl_error($ch);
    44. return false;
    45. }
    46. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    47. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    48. curl_close( $ch );
    49. return $response;
    50. }

  • 相关阅读:
    RK3568-mpp(Media Process Platform)媒体处理软件平台
    VM-Linux基础操作命令
    2022,中国TO B企业出海实录
    【国产MCU】-CH32V307-SPI控制器
    4.凸优化问题
    springboot/java/php/node/python微信小程序的在线投稿系统【计算机毕设】
    Java中如何检测HashMap中是否存在指定Key呢?
    《高考择校择专业:权衡与抉择的智慧》
    linux使用stress命令进行压力测试cpu
    翻译软件-免费翻译软件-自动批量翻译软件
  • 原文地址:https://blog.csdn.net/weixin_41692437/article/details/126973066