如何用post的方式跨域调用接口
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-02-07 13:50:18
[PHP讨论组]

如何用post的方式跨域调用接口

女神的闺蜜爱上我
女神的闺蜜爱上我

全部回复(2)
PHP中文网

使用jsonp接口的方式输出数据,也可以在服务器端加上允许域名来源设置,代码如下:

<?php
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
    'http://php.cn',
    'http://www.php.cn',
    'http://m.php.cn',
);
if(in_array($origin, $allow_origin)){
    header('Access-Control-Allow-Origin:'.$origin);
}


巴扎黑
function https_request($url,$data = null){        $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)){    
          curl_setopt($curl, CURLOPT_POST, 1);  
          curl_setopt($curl, CURLOPT_POSTFIELDS, $data);   
       }    
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
       $output = curl_exec($curl);    
       curl_close($curl);    
       return $output;
   }


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号