After two days research and some rush learning into php. I finally get a little bit exhausted and wants somehow write all the things that I know and try to ask in a stright, easy way. So maybe later some beginner can find some useful information here.
There is how I understand php proxy, which is meant to solve the cross origin/ cross domain issue.
You can't just send an ajax request to another domain (either GET or POST), it will be blocked by the browser because of the SOP (same origin policy).
But of course, you can give the url of the request direct to your browser and get the infos you need.
I understand the proxy php like this: It's like, the proxy php do the things in 2 for you, it make a request to server, get the infos, give the infos back to you. So you can handle the infos later.
I have tried the "GET" method and it works fine:
in proxy.php
:
<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix) {
$file = file_get_contents($_GET['requrl']);
}
elseif ($_POST['requrl'] != $nix) {
$file = file_get_contents($_POST['requrl'], false, $_POST['data']);
}
else {
$file = "false type";
}
echo $file;
?>
in main.js
:
var url = 'https://www.example.com/example?'
url = encodeURI(url);
$.ajax({
type: 'GET',
url: 'proxy.php',
data: {requrl: url}
}).done(function(res) {
// handle the response
})
pretty easy right? However, I still can not understand why man put the url in the "data" and created a "requrl" for it. But it' OK. because it works.
Then I wanted to try the "POST" method, in which I need to POST a xml file to a target server, and the server will do some calculation and gives me back the result. This is what I tried in proxy.php
(used everything I can find in stack overflow):
<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix) {
$file = file_get_contents($_GET['requrl']);
}
elseif ($_POST['requrl'] != $nix) {
$postdata = http_build_query(
array(
'data' => $_POST['data']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/xml',
'content' => $_POST['data']
)
);
$context = stream_context_create($opts);
$file = file_get_contents($_POST['requrl'], false, $context);
}
else {
$file = "false type";
}
echo $file;
?>
and in main.js
:
function sendPOSTRequest(xml) {
var url = 'https://www.example.com/example?';
url = encodeURI(url);
$.ajax({
type: "POST",
url: "proxy.php",
data: {
requrl: url,
data: xml
},
success: function (response) {
console.log('POST: ', response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error: ", textStatus, errorThrown);
alert("POST failed");
}
});
}
The status code for this ajax request is always 200 OK but I never get something back. Because I think the "200 OK" only means that I sent the request successfully to the proxy.php, but how the proxy.php connected the target server or if there is any mistakes in proxy.php, I don't know that.
So there are my questions:
if u see this section of codes:
$postdata = http_build_query( array( 'data' => $_POST['data'] ) );
Why I need an array here to pass the data? What I want to pass is only a xml file in string. which will be very easy to do with normal ajax. (just data: xml
)
How can I know the connect status between the proxy.php and the real target server?
How to pass the parameters right? (I have tried the POST in POSTMAN, everthing works fine. for the xml file which needs to be passed through POST, I just selected "body" => "raw" and "XML(application/xml)" in POSTMAN, but I have no idea how to pass these with php/ajax to the target server). In other words: what is the equivalent settings in javascript/php, when I tried it successfully in POSTMAN?