Curl return html code when ajax request - javascript

I using curl to send request to another page and return value to ajax, i have prevented curl redirect to another and just return value to ajax but final result i receive from ajax have contain html code, my code:
php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://abc.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$data = curl_exec($ch);
curl_close($ch);
echo 'my value';
ajax:
jQuery.ajax({
type: "POST",
url: "http://pagephp.com",
data: data,
success: function(result) {
alert(result);
}
});
alert will show: <html>...</html> my value
help me

You need to enable CURLOPT_RETURNTRANSFER to true/1 then your return value from curl_exec will be the actual result from your successful operation. In other words it will not return TRUE on success. Although it will return FALSE on failure.
http://php.net/manual/en/function.curl-exec.php
You should enable the CURLOPT_FOLLOWLOCATION option for redirects but this would be a problem if your server in in safe_mode and/or open_basedir in effect which can cause issues with curl as well.
header('Content-type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://abc.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$data = curl_exec($ch);
curl_close($ch);
echo json_encode('my value');
JQUERY
jQuery.ajax({
type: "POST",
url: "http://pagephp.com",
data: data,
success: function(result) {
alert(result);
}
});

Related

PHP cURL request to JavaScript

I need to convert this PHP cURL request to JavaScript. I'm not exactly sure how to go about doing it.
I basically need to make this request be client sided instead of server sided.
function createSite($template_id) {
//create array with data
$data = array("template_id"=>$template_id);
//turn data into json to pass via cURL
$data = json_encode($data);
print $data;
//Set cURL parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,
CURLOPT_URL,'https://api.duda.co/api/sites/multiscreen/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:
application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template_idte data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
//decode result
$output = json_decode($output);
//return unique site_name
return $output->site_name;

close php cURL only after Ajax complete

A have cURL login to a network router which is triggered by ajax. But I need to keep the connection alive until an xhr request is finished on success.
Is this somehow possible?
To clarify here is what I want to do, here is my Ajax function for the login:
function logintorouter(){
$.ajax({
type: 'GET',
dataType:"text",
url: 'login.php'
}).done(function(data){
if(data.includes('WiFi')){
setWiFichannel(); /*Wait for this function to finish before closing cURL connection */
}else{
alert('something went wrong, try again later!');
}
}).fail(function(data){
alert('can not connect to router');
});
}
and my login.php looks like this now:
<?php
function openRouter(){
$username = 'admin';
$password = 'pass';
$url="http://10.1.10.1/login/Auth";
$postinfo = "username=".$username."&password=".$password;
$cookie_file_path = "cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://10.1.10.1/main.html#WiFi");
$mainPage = curl_exec($ch);
curl_close($ch);
/*As you can see I close the connection here so the setWiFichannel() will fail
but I don't want to keep the connection alive forever*/
return $mainPage;
}
echo openRouter();
?>

PHP curl post login details

I try to login on axs.com using PHP curl but my script doesn't work.
I want small help to auth this page via PHP.
Is there error in my code?
$username = 'aUsername';
$password = 'aPassword';
$loginUrl = 'https://www.axs.com/login_check';
/* EDIT EMAIL AND PASSWORD */
$EMAIL = $username;
$PASSWORD = $password;
function cURL($url, $header=NULL, $cookie=NULL, $p=NULL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if ($p) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
if ($result) {
return $result;
} else {
return curl_error($ch);
}
curl_close($ch);
}
$a = cURL($loginUrl,true,null,"_username=$EMAIL&_password=$PASSWORD");
preg_match('%Set-Cookie: ([^;]+);%',$a,$b);
$c = cURL($loginUrl,true,null,"_username=$EMAIL&_password=$PASSWORD");
preg_match_all('%Set-Cookie: ([^;]+);%',$c,$d);
for($i=0;$i<count($d[0]);$i++)
$cookie.=$d[1][$i].";";
/*
NOW TO JUST OPEN ANOTHER URL EDIT THE FIRST ARGUMENT OF THE FOLLOWING FUNCTION.
TO SEND SOME DATA EDIT THE LAST ARGUMENT.
*/
echo cURL("http://www.axs.com/",null,$cookie,null);
?>
Is this possible on this web site?

Scraping AJAX requests with random strings appended to URL

I am trying to monitor cricket scores on scorespro/cricket by making browser AJAX requests. Analysing the network traffic in Google Chrome, I can see my browser making requests of the form:
http://www.scorespro.com/cricket/ajax.php?g_sort=league&date=2014-10-02&mut=1412265716&sut=0&(some_random_number)
When I click on the response IN Google Chrome, I can see the data that has been received. However when I try to request the request URL myself, no data is received. Why is that happening (is it to do with the random string) and how can I get around it?
Is doing this from javascript a requirement? Have you considered abstracting the requests by calling a script on a server you control?
For example on your server you could have a PHP script called, for example, "grabber.php"
<?php
$r = '0.' . rand(1000000000000000, 9000000000000000);
$url = 'http://www.scorespro.com/cricket/ajax.php?g_sort=league&date=2014-10-03&mut=1412328280&sut=0&' . $r;
$useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:32.0) Gecko/20100101 Firefox/32.0';
$referer = 'http://www.scorespro.com/cricket/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
$response = curl_exec($ch);
curl_close($ch);
$data = array('payload' => $response);
echo json_encode($data);
exit();
?>
You could then call that page via a simple ajax request :
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$.ajax({
url: 'http://yourserver.com/grabber.php',
dataType: 'json',
type: 'GET',
success: function(data, textStatus, jqXHR){
if (data['payload']){
alert(data['payload']);
} else {
alert ('oops');
}
}
});
Of course if you went with this approach you'd have to decide how to get the URL's you need to request from the cricket site to the grabber script (i.e. pass them from javascript or get them directly from within the PHP script depending on your requirements)

how to send JSON array to server with no AJAX

i am trying to send an array of JSON objects to
PHP server using an hidden field
but all i am getting in the server is just a string
this is my javascript
function CreateArrayOfJSON()
{
var all_childrens = $('#form_div').find('*');//get all root element childrens
var form_elements = {
elements: []
};
for(var i=0;i<all_childrens.length;i++)
{
var id='#'+$(all_childrens[i]).attr('id'); //get id
var style_attr=$(all_childrens[i]).attr('style'); //get style inline
var classes=$(all_childrens[i]).attr("class");
form_elements.elements.push
({
"id" : id,
"style_attr" :style_attr,
"classes" :classes
});
}
document.getElementById('form_elements_array').value=form_elements;//fill hidden field
}
this is my PHP:
this will return Object object (as in the picture above)
$form_elements=$_POST['form_elements_array'];
this will return null
$form_elements=json_decode($_POST['form_elements_array']);
any ideas ?
thank you
document.getElementById('form_elements_array').value=JSON.stringify(form_elements);
can you paste the output string ?
or alternatively
You can use Curl for the same
$url = "URL Where you want to send data";
$ch = curl_init();
$attachment = array(
'name' => 'Your first field',
'link' => 'Second Field',
'description' => 'description here',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
curl_close($ch);
now php code on the receiver page
can you paste the output string ?
or alternatively
You can use Curl for the same
$url = "URL Where you want to send data";
$ch = curl_init();
$attachment = array(
'name' => 'Your first field',
'link' => 'Second Field',
'description' => 'description here',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
curl_close($ch);
now php code on the receiver page
echo $_POST['name']; // get your data with POST method

Categories

Resources