cURL and submit button of JavaScript -> ajax? - javascript

I am using cURL to try to login in this website https://myportal.vtc.edu.hk/wps/portal/ , but i found that there is a javascript function
<"onclick="GoLogin(); return false;">
to control the login access.
so i can't connect to this website, this is my code below,
<?php
$username = 'userid';
$password = 'password';
$loginUrl = 'https://myportal.vtc.edu.hk/wps/portal';`
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'userid='.$username.'&password='.$password );
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://myportal.vtc.edu.hk/wps/myportal/!ut/p/c4/04_SB8K8xLLM9MSSzPy8xBz9CP0os3hTC39DAxNz98CwMEtTA08f50DjQEdzIwMDI_2CbEdFABEW-jM!/');
$content = curl_exec($ch);
curl_close($ch);
?>
This is the GoLogin() function :
$.ajax({
url: loginForm.attr('action'),
cache: false,
async: false,
data: params,
type: "POST",
complete: function(jqXHR, textStatus) {
if (jqXHR.status == 200) {
// login successfull
window.location = '/wps/myportal';
} else if (jqXHR.status == 399) {
// login successfull
window.location = jqXHR.responseText;
}
how can i login to this website, please help me.

This way you will not able to connect as via curl you can not execute the javascript.
For above requirement you should use the http://phantomjs.org/ .
Its php library is also available.
https://github.com/jonnnnyw/php-phantomjs
Or you can look at http://slimerjs.org/.

Related

can't get json data with api

I am using below javascript and html to get the json data. But i can't make it work with this api.It just return blank result. I have checked the api link is working so i don't think is the server problem. Please help!
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="balance"></div>
javascript
$(function() {
$.ajax({
type: "GET",
url:"https://xmg.minerclaim.net/index.php?page=api&action=public",
dataType: "json",
success: function(data) {
console.log(typeof data); // -- Object
var json = data;
$('#balance').html(json.hashrate);
}
});
});
json data
{"pool_name":"minerclaim.net","hashrate":60185.64096,"workers":1056,"shares_this_round":168700,"last_block":1531882,"network_hashrate":61752985,"fee":1,"payout":"prop"}
As I mentioned above, you are getting bool(true) in your PHP output, because the usage of var_dump().
An example of a PHP file you could use:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://xmg.minerclaim.net/index.php?page=api&action=public");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
$headers = array();
$headers[] = "Content-Type: application/json; charset=utf-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Some error:' . curl_error($ch);
die();
}
// You want your browser to think it's JSON, as we return as JSON
header("Content-Type: application/json; charset=utf-8");
echo($result);
curl_close($ch);
?>
You can copy + paste this into a new PHP file, point the $.ajax-url to that new file and see the magic happen. The example above works for me as I'm getting results back.
As for your javascript, you could also try to add contentType: "application/json", after `dataType: "json",", as this will make sure the content-type of the headers that are returned are set.

External Portal In Unifi

I am creating an external portal in unifi AC, where by users will be authenticated then after if they exist they will be allowed to use the internet connection for sometime the problem is after the user has been authenticated i got some codes which allow the user to use the internet but they are not working for me.
so The user is authenticated in the external portal but is not allowed to start
using internet the codes which should allow a user to start using internet is
in the function sendAuthentication.
The login.php source codes.
<?php session_start();
require_once('dbconnect.php');
$username = trim($_POST['username']);
$password = trim(md5($_POST['pass']));
$query = "select * from students where email='".$username."' && password='".$password."'";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($result);
if($username === $row['email'] && $password === $row['password']){
//Minutes to authorize, change to suit your needs
echo '<script language="javascript">';
echo 'alert("Successfully login")';
echo '</script>';
sendAuthorization($_SESSION['id'], (12*60));
return true;
}
else{
$_SESSION['error']=TRUE;
header('location:http://b4w.uhuruone.co.tz/guest/s/default/');
}
function sendAuthorization($id, $minutes)
{
$unifiServer = "https://41.38.13.78:8443";
$unifiUser = "bro4wote";
$unifiPass = "rt#b45w";
// Start Curl for login
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL3 only
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
// Login to the UniFi controller
curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"login=login&username=$unifiUser&password=$unifiPass");
// send login command
curl_exec ($ch);
// Send user to authorize and the time allowed
$data = json_encode(array(
'cmd'=>'authorize-guest',
'mac'=>$id,
'minutes'=>$minutes));
// Send the command to the API
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
curl_exec ($ch);
// Logout of the UniFi Controller
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
curl_exec ($ch);
curl_close ($ch);
unset($ch);
}
?>
<p>Connecting to the network...</p>
<script>
//allow time for the authorization to go through
setTimeout("location.href='http://www.Google.com'",6000);
</script>

call jquery function in a php while loop

I need to call a jquery function from a php while loop
my code
$msg_body = "Test%20SMS";
while($mobList = mysql_fetch_array($resSet))
{
$mobile = $mobList['mobile'];
if($mobile)
{
?>
<script>
$(document).ready(function()
{
var mob = <?php echo $mobile;?>;
var msgBody = <?php echo $msg_body;?>
var url = "http://example.com/abc.php?msg=TEXT&sendTo="+mob+"&message="+msgBody+"&method=sendMessage";
$.get( url, function( data )
{
alert( "SMS SEND to : "+mob );
});
});
</script>
<?php
}
}
it is running for one time, ie if more than one mobile numbers are there it will execute once and send one sms,
Alert is not working.
I used CURL,
now its OK
if($scm_mobile)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
}

PHP / JS - Echo Js function Notification after PHP curl post

I'm trying to show a Javascript function, which contains a alert/notification after my PHP Curl post went through.
Any idea how to get this working?
My Javascript:
<script>
$('body').pgNotification({
style:'bar',
message: 'added',
position:'top',
type:'success',
timeout:'6000'
}).show();
</script>
My PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'www.site.com');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'parameters to pass');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile';
$response = curl_exec($ch);
curl_close($ch);
?>
Now I want to show the Javascript Notification after the PHP curl post is done.
Sadly I can't simply use a echo 'HTMLCODE'; to show a notification. I need the timeout function in it.
I appreciate every kind of help.
function test() {
$('body').pgNotification({
style:'bar',
message: 'added',
position:'top',
type:'success',
timeout:'6000'
}).show();
}
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'www.site.com');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'parameters to pass');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile';
$response = curl_exec($ch);
curl_close($ch);
echo '<script>test();</script>';
?>
Expecting that you are not calling CURL through AJAX. Just echo your javascript function if CURL response is what you want it to be.
Javascript
function showNotification(){
$('body').pgNotification({
style:'bar',
message: 'added',
position:'top',
type:'success',
timeout:'6000'
}).show();
}
PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'www.site.com');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'parameters to pass');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile';
$response = curl_exec($ch);
curl_close($ch);
if($response == 'whatever it should return'){
echo "
<script>
// Run this if page is loaded
// Probably using jQuery?
$(document).ready(function(){
showNotification();
});
</script>";
}
?>

Using ajax instead of using a php file with curl to get a response from another webpage

I have a task to find a way to stop using the php curl, and I have to use only javascript without jQuery.
This was my php file and it was called by another ajax:
$jsonData = json_decode(file_get_contents('php://input'));
$url ='https://api.#######.com/####'; // this is not my website, just using their api
$ch = curl_init();
$data = array(
'text' => $jsonData,
'from' => 'eng',
'to' => 'fra'
);
$data_encoded = json_encode($data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: SECRET apikey=###'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_encoded);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
echo $result;
This is my new ajax but i get this error: NS_ERROR_FAILURE: Failure
function getTranslation(a_data,from,to)
{
var json_array = {};
var url = 'https://api.#####.com/#####';
var xmlhttp;
json_array = { 'text': a_data,
'from' : 'eng',
'to' : 'fra' };
var json_data = JSON.stringify(json_array);
console.log(json_data);
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("Content-type","application/json");
xmlhttp.setRequestHeader("Authorization","SECRET apiKey=###");
xmlhttp.send();
json_parsed = JSON.parse(xmlhttp.responseText);
return json_parsed.translation;
}
If i missed something please let me know I will add more details.
This sounds like it is a result of javascript's same origin policy.
You will need to use techniques likes CORS or JSONp to get around this. The link above has more details.

Categories