简体   繁体   中英

how to pass an array of data through cURL in php?

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;
?>

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM