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

    评论

报告相同问题?