Mess with the best, die like the rest… 

Facebook

PHP Curl vs Ajax – https XML secure cross domain server request

I believe many programmer have dilemma on cross domain server programming. As I stumble upon on one of my freelance project, I’ve created both version to compare which are more user friendly.

AJAX

//////////////////////
function GetXmlHttpObject()
{
var xmlHttp=null;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType('text/xml');
}
}
catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

if (!xmlHttp) {
alert(‘Giving up :( Cannot create an XMLHTTP instance’);
return false;
}

return xmlHttp;
}

//////////////////////
var xmlHttp

function main()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert (“Your browser does not support AJAX!”);
return;
}

url=”https://www.urweblinkmaa.com”;

xmlHttp.onreadystatechange=stateChanged;

try{
xmlHttp.open(“GET”,url,true);
}catch(e){
try{
xmlHttp.open(“GET”,url,false);
}catch(e){
alert(“Can’t proceed with Firefox or Mozilla. Error : ” +e.description+ “. Please use Internet Explorer.”);
}
}

try{
xmlHttp.send(null);
}catch(e){
xmlHttp.send();
}
}
/////////////////////////////////
function stateChanged()
{
// alert(xmlHttp.readyState);
if (xmlHttp.readyState==4)
{
var xmlDoc=xmlHttp.responseXML.documentElement;
var txt;
try{
txt = xmlDoc.getElementsByTagName(“urTag”)[0].childNodes[0].nodeValue;
}catch (e){
txt=xmlDoc.getElementsByTagName(“againUrTag”)[0].childNodes[0].nodeValue;
}

document.getElementById(“putVarDivHere”).innerHTML=txt;
document.getElementById(“putVarDivHere”).innerHTML=xmlDoc.getElementsByTagName(“status”)[0].childNodes[0].nodeValue;
document.getElementById(“putVarDivHere”).innerHTML=”";
}
}

PHP CURL

function open_https_url($url,$refer = "",$usecookie = false) {
$agent = "MSIE 6.0 (compatible; Mozilla/4.0; Windows NT 5.1) Opera 7.54u1 [en]";

if ($usecookie) {
if (file_exists($usecookie)) {
if (!is_writable($usecookie)) {
return “file exists. Can’t write to $usecookie cookie file, change file permission to 777 or remove read only for windows.”;
}
} else {
$usecookie = “cookie.txt”;
if (!is_writable($usecookie)) {
return “file not exists. Can’t write to $usecookie cookie file, change file permission to 777 or remove read only for windows.”;
}
}
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

if ($usecookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);
}

if ($refer != “”) {
curl_setopt($ch, CURLOPT_REFERER, $refer );
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result =curl_exec ($ch);
curl_close ($ch);
return $result;
}

preg_match(‘/([\s\S]*)<\/NameOfNode>/’,$param,$varvar);
preg_match(‘/([\s\S]*)<\/NameOfNode>/’,$param,$varvar);

Trust me..PHP CURL is more user friendly as user don’t have to change setting on IE their cross domain request. :)

 

You must be logged in , to post a comment.

Home AJAx PHP Curl vs Ajax – https XML secure cross domain server request
credit