weixin_33724059 2015-12-22 15:05 采纳率: 0%
浏览 65

在PHP中调用Ajax吗?

I was wondering can you do this call in PHP (with curl of someting?)

jQuery.ajax({
    url: this.config.url,
    type: 'GET',
    dataType: "jsonp",
    jsonp: 'callback',
    //          jsonpCallback : 'jsonp_return',
    data: {
        f:'some_function'
    }
});

Thanks in advance!

  • 写回答

2条回答

  • weixin_33738555 2015-12-22 15:08
    关注

    If you want to make an HTTP call from PHP to a URL you can do it without using curl:

    $result = file_get_contents('http://your-url');
    

    If you need to pass some parameters to the URL you can use streams:

    $data = http_build_query( ['data' => 1] );
    
    $options =
    [
        'http' =>
        [
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $data
        ]
    ];
    
    $context  = stream_context_create($opts);
    
    $result = file_get_contents('http://your-url', false, $context);
    

    In regard to JSONP: it's used to overcome the JSON 'same origin policy' for ajax requests. As from PHP you have not this limitation and you can call any URL, you don't need the JSONP 'trick': you can simply collect the data and process it

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧