Array of array in python to pass in php - php

How i create an array of array in python to pass php format correctly?
I tried to insert the ret in python also inside the json.dumps function and inside json.loads, but without results
Output in php:
{
[‘event’]=>‘Some text’
[‘data’]=>Array
(
[‘key’]=> value
)
}
In python create a dictionary:
ret = {
‘event’:’Some text’,
‘data’ :{‘key’:’value’}
}
r= request.post(“url”, ret)
But i recived this from python:
Output:
{
[‘event’]=>‘Some text’
[‘data’]=> ‘key’
}

Related

how to avoid typeerror while returning result from php to python

I have the following code in php and python,I return $result from php to a python handler and running into below error..how should I return in python so that I don't run into this error?
php
$result = [
"TRIGGER_STATUS" => "SUCCESS",
"PW_LINK" => "https://pw.company.com/"
];
Python
orderedResults = sorted(result['result'].items(), key=lambda kv: kv[1]['Order'])
Error:-
File "\\data\workspace\username\pwp4plugin\UI.py", line 269, i
n <lambda>
orderedResults = sorted(result['result'].items(), key=lambda kv: kv[1]['Orde
r'])
TypeError: string indices must be integers, not str
The issue is with kv[1]['Order'] being passed to the lambda function being passed to key. Assuming the dict which Python has after doing json.decode() is this:
>>> result = {'result': {
... "TRIGGER_STATUS": "SUCCESS",
... "PW_LINK": "https://pw.company.com/"
... }
... }
>>>
>>> # this is what .items() looks like:
... result['result'].items()
[('PW_LINK', 'https://pw.company.com/'), ('TRIGGER_STATUS', 'SUCCESS')]
So in the loop, index 1 is 'https://pw.company.com/' and 'SUCCESS':
>>> for kv in result['result'].items():
... print kv[1]
...
https://pw.company.com/
SUCCESS
So kv[1]['Order'] is attempting to lookup a key called 'Order' against each of the two strings. Whereas, what Python is expecting is an integer index to something in each of those strings. Example:
>>> for kv in result['result'].items():
... print kv[1][4] # the fifth letter in each string
...
s
E
In the PHP associative array you've given, there is no key 'Order'. So it's unclear to us what you were trying to use. What is result in Python once the PHP result is parsed?

save value taken en $_POST in an array

I'am passing values from java (android) to a web service (php) : the structure should be an array because the webservice take an array and make a serach in that array so how could I passing an array in $_POST :
$interet= $_POST['interet'];
// must be an array like this : $interet =array('piano','flute','chien');
NB/: the contain of the array is dynamic , it may have One or even ten value
You can create in Java json:
String mStringArray[] = { "piano", "flute", "chien" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
after send it to php server, and in php do this:
$array = json_decode($_POST, true);

Send an associative array from Laravel function to CasperJS

I am new to CasperJS and laravel. I need a help, I want to send an associative array(all the contents in that array at once) to CasperJS from a function, currently i am encoding it using the JSON_encode as the CasperJS script doesn't take array. The following piece of code encodes the array and sends as one string. On the other side i am fetching the string in casperJS script and unable to decode the JSON. The format of json changes on reaching the casperJS script.
$array = array
(
[1] => http://www.xxxx.com,
[2] => http://www.yyyy.com,
[3] => http://www.zzzz.com
);
$data_fetch=json_encode($array);
$casperjs = new CasperJS;
$result = $casperjs->execute($this->script2,$data_fetch);
print_r($data_fetch);
which outputs after encoding the array
{"1":"http://www.xxxx.com","2":"http://www.yyyy.com","3":"http://www.zzzz.com"}
The CasperJS script
var system = require('system');
var casper = require('casper').create({
verbose: true,
logLevel: 'error',
pageSettings: {
loadImages: false,
loadPlugins: false
}
});
var data = system.args[4];
casper.start(function() {
var decode=json.stringify(data);
});
casper.run();
when you check the input in 'data' variable using console.log it is
{1:http://www.xxxx.com,2:http://www.yyyy.com,3:http://www.zzzz.com}
which is different from the json.encode it is omitting double quotes, because of different formats i am unable to decode the content.
Can anyone help why is it doing that way?? Any solution for this?? OR is there any other better way to pass an array to casper and return back the result as an array.

Print php array in python

I have an array in php that I would like to print in python
php
return array();
python code
value=os.system("php path/to/file"); // returns array
print value
//output
0
How do I print out the values of value?

unable to unpack php array with $.post()

I have an array stored in php: $cv1 = array('a','b');
As you can see in the code below, I am trying to get the respective data from the array then delegate it to two separate functions.
But the data returned from the php callback function is: 'Array' instead of 'a','b';
and result[0] gets 'A' result[1] gets 'r' etc.
Thanks for help!
js:
$('a').on('click',function(){
var cv = $(this).data('cv');
var url= '_php/myphp.php';
$.post(url,{contentVar:cv},function(data) {
result=data;
return result;
}).done(function() {
alert(result[0]);
$('#myDiv').html(result[1]);
});
});
php:
$cv1 = array("a","b");
$contentVar = $_POST['contentVar'];
if($contentVar == "cv1")
{
echo json_encode($cv1);
}
It's common in PHP for you to get "Array" instead of an actual array when it accidentally gets cast to a string. That's not what you're doing here though; we can test:
> $cv1 = array("a","b");
array(2) {
[0] =>
string(1) "a"
[1] =>
string(1) "b"
}
> json_encode($cv1);
string(9) "["a","b"]"
$cv1 gets encoded correctly.
You must be treating it like a string somewhere else:
> (string)array("a","b")
! Array to string conversion
string(5) "Array"
Also, why do you have two success callbacks in $.post? The 3rd argument is a success callback, which works the same as .done. Use one or the other (I recommend done) but not both.
You may also consider passing json as the last argument (dataType) so that jQuery knows what to expect and will properly decode the result.
Try to do something like this:
$.post(url,{contentVar:cv}
function(response)
{
alert(response[0]);
},'json'
);
You need to convert the json string format to a javascript object/array.
var result = $.parseJSON(data);
see this question jQuery ajax request with json response, how to? for more detail.
But there is something strange that, if is returning 'Array', the php is not converting the array to json, what happens if the ($contentVar == "cv1") return false? If you will return an array to javascript you need to convert to a string (and the json format is perfect for this).

Categories