i go from my front(login.php) to my back(loginback.php) i want to send an array of data from the back to the front. When i do this all it prints is "Array", so i tried using print_r to see what was in it and it still just says "Array".
login
<?php
if(isset($_POST['submit']))
{
include_once("dbconnect.php");
$name = $_POST['name'];
$pass = $_POST['pass'];
$account = $_POST['account'];
$post = 'name='.$name.'&pass='.$pass.'&account='.$account;
$url = "#####/Middle/loginBack.php";
$ch = curl_init();//initialize
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURL_POST, true); //true = 1
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//post is the date of name value pair
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//get data bak = true
$response = curl_exec($ch); //execute and get back data
curl_close($ch);// close all curl connections
print_r($response);
?>
loginback
<?php
include ('dbconnect.php');
$name = $_POST['name'];
$pass = $_POST['pass'];
$account = $_POST['account'];
$sql = " SELECT id, Username, Password FROM studentAccounts WHERE Username = '$name' ";
$query = mysqli_query($dbCon, $sql);
$row = mysqli_fetch_row($query);
$dbid = $row[0];
$dbName = $row[1];
$dbPass = $row[2];
$num = 100;
$myArray = array($num, $name, $dbid);
echo $myArray;
?>
It's not possible to print an array with echo. In your case since you want to grab the data from another server, you would need to send data in a controlled from. JSON would work fine here.
Change your echo to something like this
echo json_encode($myArray);
then you should see the JSON string representing your array. Now on the other side, you would use json_decode to convert it back to an array.
EDIT:
Just to make it clear, if you would replace
print_r($response);
with
print_r(json_decode($response));
i do think you will see your data.
you can use the below code to send array to the desired url using post method
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => AUTHENTICATIONURL,
CURLOPT_USERAGENT => 'user Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'0' => 'ram',
'1' => 'sham',
'2'=> 'geta',
'3'=>'so on'
)
));
Try this code snippet
and $post = 'name='.$name.'&pass='.$pass.'&account='.$account;
This is not an array it's just a string
Related
I'm trying to get the details from this example (i created the code right now).
But i'm very... confused... how can i get the details of the link, then separate and send to my MYSQL database..
<?php
$ch = curl_init();
$url = "https://reqres.in/api/users?page=2";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
if($e = curl_error($ch)) {
echo $e;
}
else {
$decoded = json_decode($resp, true);
//print_r($decoded);
foreach($decoded as $key => $item) {
$array = array(
'id' => ,
'email' => ,
'first_name' => ,
'last_name' => ,
);
print_r($array);
}
}
curl_close($ch);
?>
If you call the url in your browser then you will see that the result array is present in the data field.
You may check this by printing the whole result:
print_r($decoded);
So if you like to print_r the results it should be simply
print_r($decoded['data']);
If you like to store it in your database you may walk through the array and store each item
foreach($decoded['data'] as $item) {
storeItem($item);
}
To make this work you should implement the storeItem function which accepts the array $item and stores it into your database. There are various tutorials about doing that.
i json_decode ed an array, even thought i can print_r it, i cant call its keys. Im not a pro nor nothing, so any help is welcome. I tried a lot of things, but i cant get it to work.
My goal is to present the array results i a way that is easy to read, so I want to access it in the HTML part, and not only to be printed.
the array that i get after print_r is: Array ( [0] => stdClass Object ( [pda] => 41.52258509711 [upa] => 51.345212698165 [uu] => www.chevrolet.com.ar/ ) )
<?php
$accessID = "xxxxx";
$secretKey = "xxxxx";
// Set your expires times for several minutes into the future.
// An expires time excessively far in the future will not be honored by the Mozscape API.
$expires = time() + 300;
// Put each parameter on a new line.
$stringToSign = $accessID."\n".$expires;
// Get the "raw" or binary output of the hmac hash.
$binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);
// Base64-encode it and then url-encode that.
$urlSafeSignature = urlencode(base64_encode($binarySignature));
$cols = "103079215108";
// Put it all together and you get your request URL.
$requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;
// Put your URLS into an array and json_encode them.
$batchedDomains = array('xxxxxxxx.com');
$encodedDomains = json_encode($batchedDomains);
// Use Curl to send off your request.
// Send your encoded list of domains through Curl's POSTFIELDS.
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $encodedDomains
);
$ch = curl_init($requestUrl);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close( $ch );
$contents = json_decode($content);
print_r($contents);
$pageAuthority=$contents->upa;
$domainAuthority = $contents->pda;
$theUrl = $contents->uu;
?>
<html>
<body>
<h1>MOZcape API</h1>
<ul>
<li>URL: <?php echo $theUrl; ?></li>
<li>PA: <?php echo $pageAuthority; ?></li>
<li>DA: <?php echo $domainAuthority; ?></li>
</ul>
</body>
</html>
Just had to change $contents = json_decode($content);
to $contents = json_decode($content, true); so it can be an array.
Then call it with:$pageAuthority=$contents[0]["upa"];
Instead of:$pageAuthority=$contents->upa;
The code is working fine but i am not able to insert the user data in the mysql database.
<?php
$facebookAppAuthUrl = 'https://graph.facebook.com/oauth/access_token';
$facebookGraphUrl = 'https://graph.facebook.com';
$facebookClientId = ''; // Put your App Id here.
$facebookRedirectUrl = ''; // Redirect url same as passed before.
$facebookAppSecret = ""; // Put your App Secret here.
$code = $_GET['code'];
$url =$facebookAppAuthUrl."?client_id=".$facebookClientId
."&redirect_uri=".$facebookRedirectUrl
."&client_secret=".$facebookAppSecret
."&code=".$code;
$output = urlResponse($url);
$var = strtok($output, "&");
$ApCode = strtok($var, "=");
$ApCode = strtok("=");
// This $ApCode will be used as a token to get user info from facebook.
$url = $facebookGraphUrl.'/me';
echo '<pre>';
$resposeObj = json_decode(processUrl($url,$ApCode));
var_dump($resposeObj);
echo '<pre>';
function urlResponse($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function processUrl($url,$apCode){
if(stripos($url,'?')>0)
$url = $url.'&access_token='.$apCode;
else
$url = $url.'?access_token='.$apCode;
return urlResponse($url);
}
?>
I guess the code below is wrong. I got the user data from facebook in JSON format but unfortunately I am not able to add user data in mysql using the PHP. How could we insert the json format data in mysql using php?
<?php
require('../conn.php');
$name = $url['id']['name'];
$first_name = $url['id']['first_name'];
$last_name = $url['id']['last_name'];
$hometown = $url['id']['hometown'];
{
$sql="insert into user values('','$name','$first_name','$last_name','$hometown')";
mysql_query($sql);
}
?>
<script type="text/javascript">window.location="../index.php"</script>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
Here what you need to do , first the JSON returned data needs to be converted to array as
$response = json_decode($response,true);
Now with this data you have the array and you can use print_r($response) and see how the array looks like and use the data in the query.
Hope this helps
So, apparently I can't comment without 50 rep points - so whatever. I suspect your insert statement is off. Why do you have an empty string at the beginning? I hope that's not your primary key field. I would specify my fields if I were you and leave the auto-inc field out of it so it can auto increment :)
$sql="insert into user (`name`,`firstName`,`LastName`,`homeTown`) values('$name','$first_name','$last_name','$hometown')";
I am trying to execute a url and getting its response. Following is the code that executes the curl. I want the curl execution to return me a string in $result.
<?php
$fields = array
(
'username'=>urlencode($username),
'pwrd'=>urlencode($pwrd),
'customer_num'=>urlencode($customer_num)
);
$url = 'http://localhost/test200.php';
//open connection
set_time_limit(20);
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result; //I want $result to be "Successful"
?>
This is my test200.php on localhost:
<?php
$usernam = $_POST['username'];
$pass = $_POST['pwrd'];
$customer_num = $_POST['customer_num'];
echo "Successful!";
?>
What changes do I make in test200.php? Please help.
You should use the httpcode returned by the curl execution and not rely on a string that is returned
$res = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Here - http://www.webmasterworld.com/forum88/12492.htm
Once the data is sent to test200.php do the appropriate manipulation like insert the posted values into a table and on success
echo "Successful!";
or print the same in your test200.php.. assuming you are doing an insert code in test200.php code would be like
<?php
$qry = "INSERT INTO `your_table` (`field_customer_name`, `field_username`, `field_password`) VALUES ($fields['customer_num'], $fields['username'], some_encrypt_fxn($fields['pwrd']))";
mysql_query($qry);
$err_flag = mysql_error($your_conn_link);
if($err_flag == '') {
echo "Successful!";
}
else {
echo "Failed, Error " . $err_flag;
}
?>
If the purpose of getting "Successful!" is to check if the cURL returns success then i suggest using Prateik's answer of using the returned status code
Somehow a simple print("Successful"); statement in test200.php worked well. The response i get now is as follows: HTTP Code: 0 Array ( [0] => [1] => Successful )
I'm trying to send variables from server A to server B, and back. I have everything working other then actually sending the variables from server A to server B. So i can send variables back from server B to server A but just cant send them to Server B from server A. I use JSON to send the variables back (which works fine) and i use _POST to send them to server B.
Here is my code on both Servers:
Server A
<?
require ('../refference.php');
$post_fields = array(
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
);
$ch = curl_init('http://site.com/WP/d__access.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
$data = json_decode($result);
$sponsor_first_nme = $data->sponsor_first_nme;
echo $sponsor_first_nme;
?>
Server B
<?
include ('config/wp__2135432135435135412312415456654452547534.php');
mysql_connect($hostname,$username,$password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$reference = $_POST['unq__id'];
$username = $_POST['gdi__username'];
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = '$username' AND Unique_id = '$reference'");
while($check = mysql_fetch_array($select)) {
$sponsor_email = $check["Email"];
$sponsor = $check["GDI_Username"];
$sponsor_first_nme = $check["First_Name"];
$sponsor_second_nme = $check["Last_Name"];
$sponsor_domain = $check["GDI_Domain"];
$unq_id = $check["Unique_id"];
}
$sponsor_name = "$sponsor_first_nme $sponsor_second_nme";
$result = array(
'sponsor_first_nme' => $sponsor_first_nme,
'sponsor_second_nme' => $sponsor_second_nme,
'sponsor_email' => $sponsor_email,
'sponsor' => $sponsor,
'sponsor_domain' => $sponsor_domain,
'unq_i' => $unq_id,
'sponsor_full_name' => $sponsor_name,
);
echo json_encode($result);
?>
I know that everything else works fine as I've replaced:
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = '$username' AND Unique_id = '$reference'");
WITH
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = 'myusername' AND Unique_id = '45415645154'");
So i know the problem lies within sending the variables (
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
from server A as i cant use them in the script on server B)
When i test it using the variables, I just get a blank page, but when i replace that line as mentioned above, i get the name ($sponsor_first_nme) echoed out (the expected result)
You could use serialize() to create a string representation of whatever you are trying to send.
You may have to set the post option before you set the post data.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
If not, the post data could be empty.
Send the post data as a string
$fields = array(
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
);
$field_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
Now you can send it in curl:
Edit:
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
I've got it working, well it seemed to be working the whole time...
I was just echoing it out incorrectly. I used echo $sponsor_first_nme when it was supposed to be echo "$sponsor_first_nme" (with quotes).
Really appreciate all your help though! Thanks a BUNCH!