在PHP中模拟jQuery.ajax请求 [英] Simulate jQuery.ajax request in PHP

查看:224
本文介绍了在PHP中模拟jQuery.ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在jQuery中完全模拟PHP中的AJAX请求.我当前的代码在这里:

I have to simulate AJAX request in PHP, exactly as is in jQuery. My current code is here:

原始AJAX调用(不得修改)

$.ajax({
    type: "POST",
    url: "/someFile.php",
    data: data,
    success: function(response) {
        some_code_here;
    },
    error: function() {
        some_code_here;
    }
});

当前的PHP代码-尝试在上面模拟JS的代码行为

Current PHP code - trying to simulate JS's code behaviour above

function _misc_test() {

    $data = json_decode("xxx"); // The "xxx" is placeholder for the same string, as is in data var in JS above

    $ajaxResponse = _make_post_request('/someFile.php', $data);
    print_r($ajaxResponse);
}

function _make_post_request($url, $data) {
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

不幸的是,PHP代码似乎没有生成与JS代码完全相同的数据包-这就是我所需要的.有人可以帮我吗?

And unfortunatelly, the PHP code doesn't seems to generate exactly the same packets like JS code - that's what I need. Can anyone give me a hand please?

也许很重要,JS中的data变量包含这样的复杂JS对象:

maybe it's important, that data variable in JS holds complex JS object like this one:

{"options":{"userIP":"89.102.122.16","playerType":"flash","playlistItems":[{"Type":"Archive","Format":"MP4_Web","Identifier":"209 452 80139\/0042","Title":"Nezn\u00e1m\u00ed hrdinov\u00e9","Region":"","SubtitlesUrl":"http:\/\/img2.ceskatelevize.cz\/ivysilani\/subtitles\/209\/209452801390042\/subtitles-1.txt","Indexes":null,"Gemius":{"Param":[{"Name":"materialIdentifier","Value":"209 452 80139\/0042"},{"Name":"testParam","Value":"testValue"}]}}],"previewImageURL":null}}

推荐答案

在js中:data:$('form').serialize();

in js : data: $('form').serialize();

在php中: 如何使用file_get_contents在PHP中发布数据?

$jsonstr = '{"options":{"userIP":"89.102.122.16","playerType":"flash","playlistItems":[{"Type":"Archive","Format":"MP4_Web","Identifier":"209 452 80139\/0042","Title":"Nezn\u00e1m\u00ed hrdinov\u00e9","Region":"","SubtitlesUrl":"http:\/\/img2.ceskatelevize.cz\/ivysilani\/subtitles\/209\/209452801390042\/subtitles-1.txt","Indexes":null,"Gemius":{"Param":[{"Name":"materialIdentifier","Value":"209 452 80139\/0042"},{"Name":"testParam","Value":"testValue"}]}}],"previewImageURL":null}}';

print_r(
    $data = json_decode($jsonstr ,true)
);

$data_url = http_build_query ($data);
$data_url = str_replace("amp;","",$data_url); //fix for & to &


$data_len = strlen ($data_url);
$url      = 'http://domain.com/returnPost.php';

$result =  file_get_contents ($url, false, 
    stream_context_create (
        array ('http'=>
            array ('method'=>'POST'
            , 'header'=>"Connection: close\r\nContent-Length: $data_len\r\n"
            , 'content'=>$data_url
            ))
        )
    );

print_r(
  $result
);

在returnPost.php

in returnPost.php

print_r($_POST);

这篇关于在PHP中模拟jQuery.ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆