在PHP中模拟jQuery.ajax请求

我必须在PHP中模拟AJAX请求,就像在jQuery中一样。 我目前的代码在这里:</ p>

原始AJAX调用</ strong>(不得修改)</ p>

  $。  ajax({
type:“POST”,
url:“/ someFile.php”,
data:data,
success:function(response){
some_code_here;
},
错误:function (){
some_code_here;
}
});
</ code> </ pre>

当前PHP代码</ strong> - 试图模拟上面JS的代码行为 </ p>

  function _misc_test(){

$ data = json_decode(“xxx”); //“xxx”是相同字符串的占位符,就像JS中的数据var中的

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

_function_make_post_request($ url,$ data){
$ ch = curl_init($ url);

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

$ response = curl_exec($ ch);
curl_close($ ch);
返回$ response;
}
</ code > </ pre>

不幸的是,PHP代码似乎没有像JS代码那样生成完全相同的数据包 - 这就是我所需要的。 有人可以帮忙吗?</ p>


编辑:</ em>也许重要的是, data </ code>变量在 JS拥有像这样的复杂JS对象:</ p>

  {“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 \ /字幕\ / 209 \ / 209452801390042 \ /subtitles-1.txt”, “索引”:NULL, “Gemius”  :{“Param”:[{“Name”:“materialIdentifier”,“Value”:“209 452 80139 \ / 0042”},{“Name”:“testParam”,“Value”:“testValue”}]}}  ],“previewImageURL”:null}} 
</ code> </ pre>
</ div>

展开原文

原文

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

Original AJAX call (mustn't be modified)

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

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;
}

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?


EDIT: 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}}

donglu8344812
donglu8344812 似乎没有生成完全相同的数据包是什么意思?
9 年多之前 回复
dousi1994
dousi1994 我想你应该把它作为答案。
9 年多之前 回复
dongzhen7108
dongzhen7108 对不起,我错误地发布了一个概念。现在它被编辑了。
9 年多之前 回复
duanji7182
duanji7182 为什么不使用file_get_contents?
9 年多之前 回复
douyou8047
douyou8047 你有什么问题?
9 年多之前 回复
dongqingchan2385
dongqingchan2385 你也有问题,或者你只是想分享你的经历?如果是后者,那么SO就不适合这个。
9 年多之前 回复

1个回答

在 php:
如何使用file_get_contents在PHP中发布数据 ? </ p>

  $ 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 \ /字幕\ / 209 \ / 209452801390042 \ /subtitles-1.txt”, “索引”: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函数( “放大器;”, “”,$ data_url); //修复&amp; amp; to&amp;

$ data_len = strlen($ data_url);
$ url ='http://domain.com/returnPost.php';
nnresult = file_get_contents($ url,false ,
stream_context_create(
array('http'=&gt;
array('method'=&gt;'POST'
,'header'=&gt;“Connection:close

Content-Length:$ data_len


,'content'=&gt; $ data_url
))

);

print_r(
$ result
);
</ code> </ pre>

在returnPost.php </ p>

  print_r($ _ POST); 
</ code> </ pre>
</ div>

展开原文

原文

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

in php : How to post data in PHP using file_get_contents?

$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 &amp; 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
Content-Length: $data_len
"
            , 'content'=>$data_url
            ))
        )
    );

print_r(
  $result
);

in returnPost.php

print_r($_POST);

drkrsx3135168
drkrsx3135168 $ data_url = str_replace(“amp;”,“”,$ data_url); 修复&amp; 至 &
9 年多之前 回复
duan1933
duan1933 我仍然得到同样的错误。 我会寄给你一封邮件。
9 年多之前 回复
duangu4943
duangu4943 对不起,我有同样的错误。 也许JS的数据变量的内容很重要 - 所以我编辑了原帖。 请再看一遍。
9 年多之前 回复
dtwncxs3547
dtwncxs3547 好的,谢谢 - 我会尽力报告(接受你的解决方案)它是否有效
9 年多之前 回复
立即提问
相关内容推荐