How to unwrap a div buffered with php ob_start - javascript

In the Middle of a research. How do I unwrap the div with the ID #fromWhere in the php script below. The I was able to do was addClass. Whenever I try to wrap it, it unwraps almost every other element except itself. Any suggestions on how I can achieve this.
<?php ob_start(); ?>
<div id="fromWhere" >19977</div>
<?php $contents = ob_get_contents(); ?>
In my effort to unwrap it, I changed things over to make it look like below because it work when i tried targeting it from an external script.
<?php ob_start(); ?>
<script> $('#fromWhere').unwrap(); </script>
<div id="fromWhere" >19977</div>
<?php $contents = ob_get_contents(); ?>
Everything:
PHP
<?php ob_start(); ?>
<script> $('#fromWhere').unwrap(); </script>
<div id="fromWhere" > </div>
<?php $contents = ob_get_contents(); ?>
<?php
$params = array(
'origin' => $contents,
'destination' => um_user('postal_zip_code'),
'sensor' => 'true',
'units' => 'imperial'
);
$params_string='';
// Join parameters into URL string
foreach($params as $var => $val){
$params_string .= '&' . $var . '=' . urlencode($val);
}
// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');
// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
// Parse the JSON response
$directions = json_decode($return);
//echo"<pre>";
//print_r($directions);
// Show the total distance
echo '<p><strong>Total distance:</strong> ' . $directions->routes[0]->legs[0]->distance->text . '</p>';
?>
<div id class="distance"></div>
jQuery that inserts the content into #fromWhere
$(document).ready(function() {
var from = $('#show-here #from').text();
$(".approved #fromWhere").text(from);
});

Related

preg_match not Working php and java script

I need to extract a value (id value) from html web page .
this value is included in JavaScript code.
my scraper php >
<?php
if (isset($_POST['submit']))
{
$handle = fopen($_POST['website_url'], "r");
if ($handle)
{
while (!feof($handle))
{
$text .= fread($handle, 128);
}
fclose($handle);
}
$result = preg_match('(?<=id: ")(.*)(?=",)', $text);
echo $result;
}
?>
My javaScript Code is >
<script type="text/JavaScript">
var geoInstance = gioFinder("geo");
geoInstance.setup({
id: "126568949", // i need this
geometre: "triangle",
type: "html",
image: 'https://example/126568949.jpg',
});
</script>
I need to extract id value but preg_match not working
It looks like you are missing the /s wrapping your pattern.
You will also want to set the matches array
$result = preg_match('/(?<=id: ")(.*)(?=",)/', $text, $matches);
echo $matches[0];

how can I send post data in php programatically?

I need to send post data to another page programatically.
I have 2 DateTime in Database I want to compare these DateTimes. and if one of them is bigger than another automatically send post data to another page.
this is simple code:
require('connect.php');
$sql = 'SELECT * FROM POSTS ORDER BY POSTDATETIME ASC';
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{
echo $row["ID"].".".$row["PostDateTime"]."<br/>";
$text = $row["Text"];
$d = new DateTime("now");
$d1 = new DateTime($row["PostDateTime"]);
if($d>$d1)
{
// Send Post Data to another page;
}
else
{
echo "false<br/>";
}
}
}
I googled but there is no way to send automatically post data without any form or ajax .
ajax needs to some event occurs.
And I don't have any Idea how to do that . I will appreciate any tips.
You could use CURL to send a post request.
require('connect.php');
$sql = 'SELECT * FROM POSTS ORDER BY POSTDATETIME ASC';
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{
echo $row["ID"].".".$row["PostDateTime"]."<br/>";
$text = $row["Text"];
$d = new DateTime("now");
$d1 = new DateTime($row["PostDateTime"]);
if($d>$d1)
{
$ch = curl_init('http://www.linktoyourotherpage.com');
curl_setopt($ch, CURLOPT_POSTFIELDS, $row);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
else
{
echo "false<br/>";
}
}
}
The only way to achieve what you're doing is with a intermediate page that sends the user to Page C. Here's a small/simple snippet on how you can achieve that:
<?php
if($d>$d1) { ?>
<form id="myForm" action="Page_C.php" method="post">
<?php
foreach ($_POST as $a => $b) {
echo '<input type="hidden" name="'.htmlentities($a).'" value="'.htmlentities($b).'">';
}
?>
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>
<?php } else {
echo "false<br/>";
}
?>
store the data in a global variable say for example $_SESSION and redirect to another page ... session start and use the global variable.

How to consume here.com's REST api service

Has anyone tried using here.com's rest api?
I'm trying to run an example request, but nothing happens. I'm new to REST, so I'm clueless about the problem. If the $url is plugged directly into the browsers address text box, the image is displayed, but my curl function in the code below does not retrieve anything, or so it seems.
$url = "http://image.maps.cit.api.here.com/mia/1.6/route?app_id=redacted&app_code=redacted&r0=52.5338,13.2966,52.538361,13.325329&r1=52.540867,13.262444,52.536691,13.264561,52.529172,13.268337,52.528337,13.273144,52.52583,13.27898,52.518728,13.279667&m0=52.5338,13.2966,52.538361,13.325329&m1=52.540867,13.262444,52.518728,13.279667&lc0=440000ff&sc0=440000ff&lw0=6&lc1=44ff00ff&sc1=44ff00ff&lw1=3";
$ch = curl_init($url);
$options = array(
CURLOPT_CONNECTTIMEOUT => 20 ,
CURLOPT_AUTOREFERER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$result = json_decode($response);
echo "response: <br>";
echo "<br>";
echo "result: <br> " . $result;
curl_close($ch);
I found the error in the end.. This is a working sample of using the here.com REST api that I'm posting so that anyone who is in the same situation may have a look at it. The problem was that the construction of the query string was incorrect as I just copied and pasted it from the browser. If anybody comes across the same problem, I recommend taking a look at the documentation here..
$base_url = 'http://image.maps.cit.api.here.com';
$path = '/mia/1.6/';
$resource = 'route'; // type of service... this should change if you want something else
$query_string = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXXXX', //your app id
'app_code' => 'XXXXXXXXXXXXXXXXXXXXXX', // your app code
'r0' => '52.540867,13.262444,52.536691,13.264561,52.529172,13.268337,52.528337,13.273144,52.52583,13.27898,52.518728,13.279667', // coords for the route
'lw' => '4', // line width
'lc' => '00ff00', // line color
);
$url = $base_url . $path.$resource . '?' . http_build_query($query_string);
$img_file = './img/test_img2.jpg'; // save image here
$ch = curl_init($url);
$fp = fopen($img_file, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
<img src='<?php echo $img_file;?>'>

Send array from php to js with ajax and json

I am trying to send an array from php (that I have taken from a mysql table to js). Although there a lot of examples out there I can't seem to make any of them work. The code that I have reached so far is:
php_side.php
<!DOCTYPE html>
<html>
<body>
<?php
//$q = intval($_GET['q']);
header("Content-type: text/javascript");
$con = mysqli_connect("localhost","root","","Tileiatriki");
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
//mysqli_select_db($con,"users_in_calls");
$sql="SELECT * FROM users_in_calls";
$result = mysqli_query($con,$sql);
/*while($row = mysqli_fetch_array($result)) {
echo $row['User1_number'];
echo "<br/>";
echo $row['User2_number'];
echo "<br/>";
echo $row['canvas_channel'];
echo "<br/>";
}*/
echo json_encode($result);
mysqli_close($con);
?>
</body>
</html>
test_ajax.html
$(document).ready(function(){
$.getJSON('php_side.php', function(data) {
$(data).each(function(key, value) {
// Will alert 1, 2 and 3
alert(value);
});
});
});
This is my first app that I use something like this, so please be a little patient.
Right now you're sending the complete page markup mixed with your json response, which of course will not work.
For example imagine that you have the following php script which suppose to return a json response:
<div><?php print json_encode(array('domain' => 'example.com')); ?></div>
The response from this page would not be json since it also will return the wrapping div element.
You can move your php code to the top of the page or simply remove all the html:
<?php
// uncomment the following two lines to get see any errors
// ini_set('display_errors', 1);
// error_reporting(E_ALL);
// header can not be called after any output has been done
// notice that you also should use 'application/json' in this case
header("Content-type: application/json");
$con = mysqli_connect("localhost","root","","Tileiatriki");
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql="SELECT * FROM users_in_calls";
$result = mysqli_query($con,$sql);
// fetch all rows from the result set
$data = array();
while($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
mysqli_close($con);
echo json_encode($data);
// terminate the script
exit;
?>

Accessing and printing HTML source code using PHP or JavaScript

I am trying to access and then print (or just be able to use) the source code of any website using PHP. I am not very experienced and am now thinking I might need to use JS to accomplish this. So far, the code below accesses the source code of a web page and displays the web page... What I want it to do instead is display the source code. Essentially, and most importantly, I want to be able to store the source code in some sort of variable so I can use it later. And eventually read it line-by-line - but this can be tackled later.
$url = 'http://www.google.com';
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_data($url); //print and echo do the same thing in this scenario.
Consider using file_get_contents() instead of curl. You can then display the code on your page by replacing every opening bracket (<) with < and then outputting it to the page.
<?php
$code = file_get_contents('http://www.google.com');
$code = str_replace('<', '<', $code);
echo $code;
?>
Edit:
Looks like curl is actually faster than FGC, so ignore that suggestion. The rest of my post still stands. :)
You should try to print the result between <pre></pre> tags;
echo '<pre>' . get_data($url) . '</pre>';
I rewrote your function. The function can return the source with lines or without lines.
<?php
function get_data($url, $Addlines = false){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
$content = htmlspecialchars($content); // Prevents the browser to parse the html
curl_close($ch);
if ($Addlines == true){
$content = explode("\n", $content);
$Count = 0;
foreach ($content as $Line){
$lines = $lines .= 'Line '.$Count.': '.$Line.'<br />';
$Count++;
}
return $lines;
} else {
$content = nl2br($content);
return $content;
}
}
echo get_data('https://www.google.com/', true); // Source code with lines
echo get_data('https://www.google.com/'); // Source code without lines
?>
Hope it gets you on your way.
Add a header Content-Type: text/plain
header("Content-Type: plain/text");
Use htmlspecialchars() in php to print the source code.
In your code, use
return htmlspecialchars($data);
instead of
return $data;

Categories