ajax, json, external url load data - javascript

i have a problem, help please
i have 2 sites and i want send data each other
first site :
var site_adres = $(location).attr('href');
var myArray = site_adres.split('/');
var site_adi_al = myArray[2];
$.getJSON('xxx.com/site/admin.php?site_adres='+ site_adi_al +'',
{},
function (data) {
$.each( data, function ( i, val ) {
var id=val['id'];
var site_adi=val['site_adi'];
$(".site_adi").append('<li>'+id+' >> <a href="'+site_adi+'"
target="_blank">'+site_adi+'</a></li>');
});
second site:
$site_adi = $_GET["site_adi"];
/* query */
$query = mysql_query("SELECT * FROM site WHERE site_adi = '$site_adi'");
if ( mysql_affected_rows() ){
$row = mysql_fetch_object($query);
$json = array(
"id" => $row->id,
"site_adi" => $row->site_adi
);
}else{
$json["hata"] = "Nothing !";
}
header("access-control-allow-origin: *");
echo json_encode($json);
result zero, what is wrong, help please

You have two basic problems (aside from the security issues explained in the comments on the question).
You are sending site_adres but reading $_GET["site_adi"]. You can't use different names for the same thing without explicitly writing code to link them somehow.
You are looping over data with $.each( data, function ( i, val ) { as if it was an array of objects, but your PHP is only sending a single object (which isn't in an array). You should be accessing the properties of data directly and not using each or val.

You should set up CORS on your webservers to allow them to fetch data from each other, since you're using php, i'm gonna assume you're using apache:
Header set Access-Control-Allow-Origin "*"
replace the * with the ip adress of your other website and vice versa.

Related

Detect URl pattern

I have data collecting software.
Data: site visits/views.
So i have a lot views data: page url, date, visitor info.
Most of URLs is just different filters or something like. I.e URLs same but it have dynamic parameters.
For example:
site1.com/?search=something
site1.com/?search=some_word
site1.com/?search=hello
site1.com/?search=world
Should be "detected" as site1.com/?search={variable}
So that is a question:
Any algorithms to auto-detect patterns of URL?
Or some analyzing classes/functions? Any programming language.
Need solution that can process big batches of URLs.
Wihout any manual pattern defining(coz i dont know it and cant do it manually for many different sites).
UPD
For example:
I have many different URLs. From many sites. I dont know how these sites work. So i need to get for example 500 URLs from one site then compare and group it by common part to get 10 unique urls as result. Which should be automatically merged via replacing with {var} any dynamic URL parts.
I think you won't get much out of a simple pattern, and have to write partially Complex algorithm something along the line of:
break each URI to it's parts: domain, page, Query-String (as keys-values)
group all URIs from same domain
if there is a page, group by that too. (most sites today use url rewrite rules so there isn't a real "PAGE")
here come the "hard part":
Match Query String Variables between the grouped URIs
if a Var is matching All(almost, all) uris, it might be meaningful to the content.
if all (almost) have the same value, it might be smth less meaningful...
note: you should also pre-check some common VarIds like: search, q,query, id,itemId, etc...
One last thing, today, as i mentioned, parts of the URL (aside from queryString) can infer dynamic parameters (e.g. Ebay items: www.ebay.com/itm/9125483; www.ebay.com/itm/{itemId})
but hey, that's why you are paid for, to think about all those issues :p
Good luck.
Here is some kind Proof of Concept :)
Example of splitting URL by "?"
Parse parameters.
Calculate frequency for unique parameter values.
Get Nth percentile.
Build URLs and replace parameters which frequency is more than Nth percentile
For small data like here in sandbox 50 percentile is enough to group some URL.
For "big real data" 90-95 percentile.
For example: I use 90 percentile for 5000 links -> result ~200 links
<?php
$stats = [];
$pages = [
(object)['page' => 'http://example.com/?page=123'],
(object)['page' => 'http://example.com/?page=123'],
(object)['page' => 'http://example.com/?page=123'],
(object)['page' => 'http://example.com/?page=321'],
(object)['page' => 'http://example.com/?page=321'],
(object)['page' => 'http://example.com/?page=321'],
(object)['page' => 'http://example.com/?page=qwas'],
(object)['page' => 'http://example.com/?page=safa15'],
]; // array of objects with page property = URL
$params_counter = [];
foreach ($pages as $page) {
$components = explode('?', $page->page);
if (!empty($components[1])) {
parse_str($components[1], $params);
foreach ($params as $key => $val) {
if (!isset($params_counter[$key][$val])) {
$params_counter[$key][$val] = 0;
}
$params_counter[$key][$val]++;
}
}
}
function procentile($percentile, $array)
{
sort($array);
$index = ($percentile/100) * count($array);
if (floor($index) == $index) {
$result = ($array[$index-1] + $array[$index])/2;
} else {
$result = $array[floor($index)];
}
return $result;
}
$some_data = [];
foreach ($params_counter as $key => $val) {
$some_data[$key] = count($val);
}
$procentile = procentile(90, $some_data);
foreach ($pages as $page) {
$components = explode('?', $page->page);
if (!empty($components[1])) {
parse_str($components[1], $params);
arsort($params);
foreach ($params as $key => $val) {
if ($some_data[$key] > $procentile) {
$params[$key] = '$var';
}
}
arsort($params);
$pattern = http_build_query($params);
$new_url = urldecode('?'.$pattern);
if (!isset($stats[$new_url])) {
$stats[$new_url] = 0;
}
$stats[$new_url]++;
}
}
arsort($stats);
I think what OP wants is regex, first you find the domain part in the url using regex, then you can remove the domain part and anything after the matched parts will remain (aka patterns).
for instance,
/^\w*.\w*(.\w*)?/\?search=/
will match the domain part in the url up to the ?search= part, then if you remove them from the whole urls you gonna get the pattern.
however i think it will match all domain-like strings in the url so you might want to change this so you don't remove the necessary part
edited for grammar and stuff
Unfortunately, I think you're out of luck without using pattern matching. You could use a library, or someone else's code for now, but there are just too many variations to solve this problem otherwise. Try this on for size:
function getURLQueryString(url) {
var query_list = {};
var query_strings = url.match(/.*\?(.*)/)[1].split('&');
var i, param;
for(i in query_strings) {
param = query_strings[i].split('=');
query_list[ param[0] ] = param[1];
}
return query_list
}
You'll get back an object in which every key-value pair is a parameter from the query string.

unable to get json data from server

My server side code is:
$bus = array(
'meaning' => $name
);
$jsonstring = json_encode($bus);
echo $_GET['callback'].'(' . $jsonstring. ')';
the value displayed on the screen is correct - ?word=heart({"meaning":"heart"})
but when I am reading it with following code its printing the value of meaning as 11200665987893779229_1460521505942
$(document).ready(function(){
$.getJSON('http://mydomain?callback=?','word=heart',function(res){
document.getElementById('print').innerText=''+res.meaning;
});
});
but when I do this:
$bus = array(
'meaning' => 'heart'
);
it's printing the correct value i.e heart
I am not getting why this is happening and how to get the correct value (I am accessing data from my different domain).
JSON.parse() converts any JSON String passed into the function, to a JSON Object.
$(document).ready(function(){
$.getJSON('http://mydomain?callback=?','word=heart',function(res){
obj = JSON.parse(res);
document.getElementById('print').innerText=''+obj.meaning;
});
});
a similar post is here

Extra slash comes in json array

I works on nestable drag and drop. When I drag and drop tiles It generate an array in textarea which is [{},{"id":267},{"id":266}]. Now When I post this array in action page then It posted [{},{\"id\":267},{\"id\":266}]. Why this extra slash comes in array. In action page I convert this array using json_decode. Now How I remove this slash from array or how I ignore this array that I successfully decode this array through jsondecode.
$(document).ready(function()
{
var updateOutput = function(e)
{
var list = e.length ? e : $(e.target),
output = list.data('output');
if (window.JSON) {
output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
} else {
output.val('JSON browser support required for this demo.');
}
};
// activate Nestable for list 1
$('#rightservices').nestable({
group: 1
})
.on('change', updateOutput);
// output initial serialised data
updateOutput($('#rightservices').data('output', $('#siteservices')));
//$('#nestable3').nestable();
});
Sounds like Magic Quotes is set on the server. This is an old, deprecated, feature of PHP where any request data would be automatically escaped with slashes regardless of what is was. You can follow the instructions listed here to disable them. From that page, any of these should work, depending on what you have access to:
In php.ini
This is the most efficient option, if you have access to php.ini.
; Magic quotes for incoming GET/POST/Cookie data.
magic_quotes_gpc = Off
In .htaccess
If you don't have access to php.ini:
php_flag magic_quotes_gpc Off
At runtime
This is inefficient, only use if you can't use the above settings.
<?php
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
?>
The below will remove the first object in the array but doesn't really solve the real issue of why it is being added in the first place?
var arr = [{},{\"id\":267},{\"id\":266}];
arr.splice(0,1);

Translate php soap request to javascript

I need to send a soap request to a web service using javascript, i was given this php example code, but i do not want to learn the language yet just just for one piece of code.
$uuid = "xxxx";
$param = array("uuid"=>new SoapVar($uuid,
XSD_STRING,
"string",
"http://www.w3.org/2001/XMLSchema")
);
$key1 = implode("", $wsdatek);
$keys = array('key' => new SoapVar(sha1($key1),
XSD_STRING,
"string", "http://www.w3.org/2001/XMLSchema")
);
$datosws = array("local_cert" => "../cert/www.page.com.pem",
"trace" => true, "exceptions" => true
);
This question is derived from the unsuccessful research made on this one
Did one example think this is what you want? Assume you can work out the rest on your own.
var datosws = [["local_cert","../cert/www.page.com.pem"],["trace",true],["exceptions",true]];
They keys var incase you get confused
var XSD_STRING = ""; //const
var key1 = ""; // sha1 the key
var keys = [["key",key1],XSD_STRING,"string","http://www.w3.org/2001/XMLSchema"];
In case you are using jQuery on top of you javascript, you should try this plugin:
https://github.com/doedje/jquery.soap

javascript arrays on gmaps

i'm newbie in javascript so, in this example exists the geometrycontrols.js (for global controls) and markercontrol.js (for marker controls)
my problem is identify the arrays where "data" is saved...
at the reference i see a savedata function but i have no idea how work with this function...
on the other side, in test.html if i've the outup on the Glog startup and output "data", and let me thinking that comes from array...
My objective is save the coordinates and other all atributes to mysql database, and when i discover where are "data" is the easy part.
if someone worked with this example (or not) can help me i'm grateful
ps: i'm really a newbie on javascript :P
edit1:
I was out for a time, and now I focus in geometrycontrols.js specially in: GeometryControls.prototype.saveData = function(opts){
var me = this;
if(opts.allData === true){
//me.saveAllData();
} else {
//construct a json data record
var geomInfo = opts.geomInfo, index = opts.geomInfo.index;
var record = geomInfo.storage[index];
var recordJSON = {};
recordJSON.type = record.type;
recordJSON.coordinates = [];
//determine geometry type, and copy geometry appropriately
if(record.type === "point"){
recordJSON.coordinates.push({lat:record.geometry.getLatLng().lat(),lng:record.geometry.getLatLng().lng()});
alert(recordJSON.coordinates);
} else {
alert("is not point");
var vertex;
for(var i=0;i<record.geometry.getVertexCount();i++){
vertex = record.geometry.getVertex(i);
recordJSON.coordinates.push({lat:vertex.lat(),lng:vertex.lng()});
}
}
//add title and description
recordJSON.title = record.title[0];
recordJSON.description = record.description[0];
//TODO add styles
recordJSON.style = ""; //TODO} //TODO Make separate prototype function?function postData(data){
//TODO
me.debug(data);
//alert(recordJSON.coordinates);
//alert(data);
};postData(me.serialize(recordJSON));}; `
When I alert(recordJSON.coordinates), the outupt is [object Object] and i've no idea why, in theory this array contains the coordinates...
Here is some code I have used to send the data to MySQL. It uses a little bit of jQuery to do the ajax magic (the line starting with the dollarsign is jQuery).
function postData(data){
me.debug(data);
var dataString = JSON.stringify(data);
me.debug(dataString);
$.post('storage.php', { data: dataString });
};
postData(recordJSON);
As you can see I've modified the way the 'recordJSON' object gets sent to the postData function a bit too: I've removed the serialise function.
Next, create a PHP file (called 'storage.php' in my case) and put this in it:
<?php
$received = json_decode($_POST['data'], true);
echo "just received " . $received['name'];
?>
You now have an array in PHP that you can do with as you please.
In the examplecode above I've modified the jQuery post function a bit, so if it doesn't work, look there.
The data is stored in JSON format in this file: http://gmaps-utility-library-dev.googlecode.com/svn/trunk/geometrycontrols/examples/data/testdata.js -- it's pretty much self-documenting, just follow the example to set your coordinates.
Note that if you need to find the latitude and longitude for a given address this is a good site: http://itouchmap.com/latlong.html

Categories

Resources