To have AWK's `-F, '{ print $1 }' in PHP - php

I run unsuccessfully
$cookie = "masi#gmail.com,777";
$cookie_tripped = trim(",", $_COOKIE['login']);
echo "Cookie: "$cookie_tripped[0];
It gives me
Cookie:
How can you do the following command in PHP?
awk -F, '{ print $1 }'

$cookie = "masi#gmail.com,777";
$cookie_tripped = explode(",", $cookie);
echo "Cookie: " . $cookie_tripped[0];

Related

Converting command-line cURL where URL is piped to python

I have this command-line cURL example:
curl -X PUT -H "X-Auth-Token: $AUTH_TOKEN" \
-d "{\"data\":{\"name\":\"Device1 Callflow\", \"numbers\":[\"1001\"], \"flow\":{\"module\":\"device\",\"data\":{\"id\":\"$DEVICE_ID\"}}}}" \
http://ip.add.re.ss:8000/v2/accounts/$ACCOUNT_ID/callflows | python -mjson.tool
My task is to convert this into a PHP function, where the needed values are passed into the function:
function someFunc($AUTH_TOKEN, $ACCOUNT_ID, $DEVICE_ID, $numbers) {}
Normally, I use this sort of code:
function setLimits($auth_token, $accountID, $feature1_cnt, $feature2_cnt) {
$service_url = "http://ip.add.re.ss:8000/v2/accounts/$accountID/limits";
$data = '{"data":{"feature1": ' . $feature1_cnt . ',"feature2": ' . $feature2_cnt . '}}';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Auth-Token: $auth_token",'Content-Type: application/json', 'Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
$decoded = json_decode($response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
return(true);
}
BUT, for a specific API call, I need to pass this as the service URL:
http://ip.add.re.ss:8000/v2/accounts/$ACCOUNT_ID/callflows | python -mjson.tool
How do I do that?
First, change:
$data = '{"data":{"feature1": ' . $feature1_cnt . ',"feature2": ' . $feature2_cnt . '}}';
to:
$data = [
"data" => [
"feature1" => $feature1_cnt,
"feature2" => $feature2_cnt
]
];
You will make a mistake typing out JSON, and it won't be easy to find or fix.
Second, python -mjson.tool
json.tool [...] to validate and pretty-print:
PHP's JSON validation is pretty much just checking if json_decode() returned NULL, and then you can check json_last_error() and json_last_error_msg() for error info. It is not robust.
You can encode JSON in "pretty-printed" format with:
json_encode($foo, JSON_PRETTY_PRINT);
But I can't think of an earthly reason why you would want to do that other than if there are human eyeballs that are going to be looking at it.
Lastly, in answering your question I've realized that I have no actual idea what you're getting at and you really need to clarify why you think you need to do anything you asked about. [other than the validation, I suppose]

No output from neo4j query

I am trying to execute the sort of example Neo4j code as found on here:
http://neo4j.com/docs/developer-manual/current/cypher/clauses/create/#create-create-a-full-path however I seem to be missing something obvious. What I need created is (A) has (B).
$n4 = 'curl -H "Accept: application/json; charset=UTF-8" -s -u user:pass -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d \'%s\'';
function basicQuery($query){
global $n4;
$str = '{"query" : "'.$query.'","params" : {}}';
return sprintf($n4,$str);
}
$A = array('Label'=>'Attributes');
$B = array('Label'=>'Attributes');
$query = 'CREATE p =(A '.json_encode($A).')-[:HAS]->(B '.json_encode($B).') RETURN p';
echo shell_exec(basicQuery($query));
But I don't get any output and when I run:
shell_exec(basicQuery('MATCH (A) RETURN DISTINCT count(A) AS tally'));
I get a tally of 0. I'm very new to neo4j so please can someone tell me what I'm doing wrong?
Ok the issue here appeared to be CURL. By using placeholders within my query such as A {Name:{Placeholder}} this seemed to generate output. The solution to solving problems like this appears to be copying the output into the command line and try it out there as well as in PHP.
1) You can't use quote around property key name
2) You need correctly encode a query
$n4 = 'curl -H "Accept: application/json; charset=UTF-8" -s -u user:pass -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d \'%s\'';
function basicQuery($query){
global $n4;
$json = array(
"query" => $query,
"params" => new stdClass
);
$str = json_encode($json);
return sprintf($n4, $str);
}
function objToMap($obj) {
$tmp = [];
foreach($obj as $key=>$value) {
$tmp[] = '`' . $key . '`: ' . json_encode($value);
}
return '{' . join(',', $tmp) . '}';
}
$A = array('Label'=>'Attributes');
$B = array('Label'=>'Attributes');
$query = 'CREATE p =(A:TEST ' .objToMap($A).')-[:HAS]->(B:TEST '.objToMap($B).') RETURN p';
echo shell_exec(basicQuery($query));

How to specify --form parameter and PUT argument in php curl ?

I have to execute this curl command in php:
curl --digest -u YourApiKey:YourApiSecret "http://api.moodstocks.com/v2/ref/YourID" --form image_file=#"image.jpg" -X PUT
So far I have this:
function ms_addimage($file, $hash_id){
$postdata = array("image_file" => "#/".realpath($file));
$opts[CURLOPT_URL] = $this->API_BASE_URL . "ref/".$hash_id;
$opts[CURLOPT_VERBOSE] =1;
$opts[CURLOPT_POST] =true;
$opts[CURLOPT_POSTFIELDS] =$postdata;
$ch = curl_init();
curl_setopt_array($ch, $opts);
$raw_resp = curl_exec($ch);
echo "Response " . $raw_resp . "\n";
curl_close($ch);
}
The file path is correct but I am missing something.
How do I pass the --form parameter and the PUT argument?
So I was missing the following:
$opts[CURLOPT_CUSTOMREQUEST] = "PUT";
Now it is working.

Send php array of device tokens to Urban Airship push

I am very suck. I am trying to send to a php array of device id's with urban airship. I am using the first example found here. Everything works, with "audience"=>"all". Every registered device gets hit. I need to make a query of a database, that has a bunch of device id's in it, and send to those device id's. What do I change "audience"=>"all" to so I can do that. I have tried everything!
Here is the code incase the link breaks:
<?php
define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$contents = array();
$contents['badge'] = "+1";
$contents['alert'] = "PHP script test";
$contents['sound'] = "cat.caf";
$notification = array();
$notification['ios'] = $contents;
$platform = array();
array_push($platform, "ios");
$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);
$json = json_encode($push);
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
echo $content; // just for testing what was sent
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
echo "Got negative response from server, http code: ".
$response['http_code'] . "\n";
} else {
echo "Wow, it worked!\n";
}
curl_close($session);
?>
It depends on what device OS you are trying to send to. Via their documentation here:
http://docs.urbanairship.com/reference/api/v3/push.html#atomic-selectors
you will need to set the correct device type to it's corresponding ID. For example:
android:
"audience" : {
"apid" : "b8f9b663-0a3b-cf45-587a-be880946e880"
}
ios:
"audience" : {
"device_token" : "C9E454F6105B0F442CABD48CB678E9A230C9A141F83CF4CC03665375EB78AD3A"
}
I found a possible solution for this from urban airship help center... They suggest this. And its working for me.
You can send to multiple device tokens or APIDs in a single request. I would suggest using our new API v3 and batching up your requests. There are a couple ways to do this:
1) Send to multiple devices in one payload
curl -v -X POST -u "<AppKey>:<MasterSecret>" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '{"audience" : {"OR": [{"device_token":"<DeviceToken1>"}, {"device_token":"<DeviceToken2>"}, {"device_token":"<DeviceToken3>"}]}, "notification" : {"alert" : "Hello iOS devices!"}, "device_types" : ["ios"]}' https://go.urbanairship.com/api/push/
OR
2) Put multiple payloads together in one batch
curl -v -X POST -u "<AppKey>:<MasterSecret>" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '[{"audience": {"device_token": "<DeviceToken1>"}, "notification": {"alert": "Hello, I was sent along with a batch of other pushes!"}, "device_types": ["ios"]}, {"audience": {"device_token": "<DeviceToken2>"}, "notification": {"alert": "I was also sent with a batch of other pushes!"}, "device_types": ["ios"]}, {"audience": {"device_token": "<DeviceToken3>"}, "notification": {"alert": "Me three!"}, "device_types": ["ios"]}]' https://go.urbanairship.com/api/push/
Switched to the PHP 2 library for Urban Airship and I was able to send to individual device tokens. I was also able to read tokens out of an array, and assign the array value as the target. Version 2 found here.

php doesn't get the json values i send from curl

curl -i -X POST -d 'json={"Amount":1000,"idPlayers":1}' http://www.myurl.com/updateamount.php
My php code:
<?php
$json = urldecode($_GET['jsonSendData']);
$json = str_replace("\\", "", $json);
$data = json_decode($json, true);
// if i hard code it here it works fine, but need from a post from curl
//$json = '{"Amount":1000,"idPlayers":1}';
$data = json_decode($json, true);
echo "json = ", $json;
$amount = mysql_real_escape_string($data['Amount']);
$id = mysql_real_escape_string($data['idPlayers']);
echo "Amount = ",$amount;
return;
Try to change to read post data and using the correct name:
$json = urldecode($_POST['json']);

Categories