php - Convert string to array in API call -


i passing array string in parameter api in php this:

http://xx.xx.xx.xx/api.php?query="array(done = 1)" 

in api file, have used array hit mongodb query:

$query = $_request['query']; $cursor = $collection->find($query); 

but didn't work. when hard-coded array(done = 1) find query, seems work fine.

if (array('done' => 1) == $query) {   echo "y"; } else {   echo "n"; } 

the above code prints n. guess it's because $query being passed string.

ps: tried json_encode, json_decode , unserialize didn't work. might doing omething wrong here.

well make bit change in query string, passing in api request.

suppose belowis array.

$array = array('done' => 1, 'message' => 'success'); 

use array_map_assoc function customization, make easy implode associative array

function array_map_assoc( $callback , $array ){   $r = array();   foreach ($array $key=>$value)     $r[$key] = $callback($key,$value);   return $r; } 

generate data sent in api our data

$querystring = implode('&',array_map_assoc(function($k,$v){return "$k=$v";},$array)); 

now send data api

$url = "http://xx.xx.xx.xx/api.php?" . $querystring ; 

now use print_r($_get) in api page , receive data below

array (     [done] => 1     [message] => success ) 

this make code easy handle , use in either if condition or sql query.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -