Want to manage mongoldb user on CodeIgniter using Parse Rest API - php

Database: Mongodb
Backend Framework: CodeIgniter
Want to manage default User table from backend. I am using ParseRestAPI but as you all know Mongodb does not allow to update user without session. So is there any way i can manage users created by App and delete/create new users.

Yes you can do that with the Parse Rest API
For exmaple to delete a user:
Use the DELETE http verb and call something like that:
Note: You need to pass the Session Token or Master Key
curl -X DELETE \
-H "X-Parse-Application-Id: YOURAPPID" \
-H "X-Parse-REST-API-Key: YOURAPIKEY" \
-H "X-Parse-Session-Token: SESSIONTOKEN" \
-H "X-Parse-Master-Key: YOURMASTERKEY" \
https://api.example.com/1/users/<objectId>
In php you can write:
$ch = curl_init('https://api.example.com/1/users/<objectId>');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Parse-Application-Id: YOURAPPID',
'X-Parse-REST-API-Key: YOURAPIKEY',
'X-Parse-Master-Key: YOURMASTERKEY'
);
$result = curl_exec($ch);
To create a user call the API like that (I use curl for example)
curl -X POST \
-H "X-Parse-Application-Id: YOUAPPID" \
-H "X-Parse-REST-API-Key: YOURAPIKEY" \
-H "Content-Type: application/json" \
-d '{ "objectId": "", "updatedAt": "", "createdAt": "", "name": "John Doe", "pass": "toto42" }' \
https://api.example.com/1/classes/User/

Related

Cloudflare security level is not changing from api

I am trying to change the security_level from curl for cloudflare api
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/zoneId/setings/security_level");
curl_setopt($ch, CURLOPT_HEADER, array('X-Auth-Email'=>'Email','X-Auth-Key'=>'Api_key','Content-Type'=>'application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('value' => 'MEDIUM'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
But i am getting error as given below:
{"success":false,"errors":[{"code":7003,"message":"Could not route to /zones/zoneId/settings/security_level, perhaps your object identifier is invalid?"},{"code":7000,"message":"No route for that URI"}],"messages":[],"result":null}
I got the same error when I tried to change security level through api. I was wondering how GET gives me correct result and PATCH command fails.
For example:
curl -v -X GET https://api.cloudflare.com/client/v4/zones/my_zone_id/settings/security_level \
-H "X-Auth-Email: my_email" \
-H "Content-Type:application/json" \
-H "X-Auth-Key: my_Global_API_Key" | jq
was giving me:
{
"result": {
"id": "security_level",
"value": "high",
"modified_on": "2019-02-07T17:05:58.073422Z",
"editable": true
},
"success": true,
"errors": [],
"messages": []
}
but:
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/my_zone_id/settings/security_level" \
-H "X-Auth-Email: my_email" \
-H "Content-Type:application/json" \
-H "X-Auth-Key: my_Global_API_Key" \
--data '{"value":"under_attack"}'
was giving me:
{"success":false,"errors":[{"code":7003,"message":"Could not route to \/zones\/my_zone_id\/settings\/security_level, perhaps your object identifier is invalid?"},{"code":7000,"message":"No route for that URI"
Somehow I realized that the issue was in copy/paste. When I re-enter the PATCH command again, everything worked.

I can't retrieve the intent from my wit.ai call

I am starting to use Wit.ai to enhance a small bot I made. I am able to make a request to the wit.ai by doing:
function sendToWitAI($query){
$witRoot = "https://api.wit.ai/message?";
$witVersion = "20170822";
$witURL = $witRoot . "v=" . $witVersion . "&q=" . $query;
$ch = curl_init();
$header = array();
$header[] = "Authorization: Bearer xxxxxxxx";
curl_setopt($ch, CURLOPT_URL, $witURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return $server_output;
}
However, when receiving the output I just get the same message I sent. For example, if the user types "I want to make a reservation" my $server_output is now "I want to make a reservation" after all that chunk of code above.
Still, I know it reaches wit successfully because I can see it in the logs there and I know the bot says (from wit.ai):
{
"confidence": null
"action": null
"type": "action"
}
On top of this, if I just do a curl with the same query:
curl -XPOST 'https://api.wit.ai/converse?v=20170822&session_id=123abc&q=I%20want%20to%20make%20a%20reservation' \
> -H "Content-Type: application/json" \
> -H "Accept: application/json" \
> -H 'Authorization: Bearer xxxxxxxx'
I get the following output:
{
"confidence" : null,
"type" : "action",
"action" : null,
"entities" : {
"contact" : [ {
"suggested" : true,
"value" : "reservation",
"type" : "value",
"confidence" : 0.95062723294726
} ],
"intent" : [ {
"confidence" : 0.98638622681962,
"value" : "make_reservation"
} ]
}
}
I'm not sure where my error is or what I'm missing to properly handle use of the value like I need.
I've been googling non-stop but I can't find anything after they (wit.ai) deprecated "stories" and there's seldom anything about handling the response.
You're using 2 different end points: /message and /converse.
The log you pasted is from /converse so I'm not even sure your first call went through. Can you try a curl to /message like this
curl -XGET 'https://api.wit.ai/message?v=20170307&q=I%20want%20to%20make%20a%20reservation' \
-H 'Authorization: Bearer $TOKEN'

Translate command-line cURL to PHP

I have a working cURL request I want to translate to PHP code:
curl \
-X POST \
-u live_abcdefg: \
-d '{
"recipient": {
"address": "test1#test.com"
}
}' \
https://api.sendwithus.com/api/v1/drip_campaigns/abcdefgh/activate
Here is the PHP code I'm trying:
<?php
$theurl = "https://api.sendwithus.com/api/v1/drip_campaigns/abcdefgh/activate";
$ch = curl_init($theurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // -X
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "recipient": { "address": "test2#test.com" } }'); // -d
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'live_abcdefg:'); // -u
$response = curl_exec($ch);
var_dump($response); // prints bool(true)
?>
EDIT: Sendwithus (the app that I'm sending requests to) shows that the second request didn't come through (second recipient test2#test.com wasn't saved).
What am I doing wrong?
What can I change in the PHP code to start debugging this issue?

How to pass array of arguments to cURL in command line?

I got desired response when i send cURL request from my PHP script.
My request is like this.
$data = array ("[product[name]]" => "nw",
"[product[handle]]" => 150,
"[product[interval_unit]]" => "day",
"[product[interval]]" => 1,
"[product[price_in_cents]]" => 0,
"[product[initial_charge_in_cents]]" => 14200,
"[product[return_url]]" =>"http://mytrial.com/office/selfie/themes/adcampaign/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d",
"[product[return_params]]" => "id={subscription_id}&customer_id={customer_id})");
$url="http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, 'sdfkjas2kjsd:x');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
It's working properly. I want to do the same request in command line.First i json encoded the array and i tried with this commands
curl -u sdfkjas2kjsd:x -H Accept:application/json -H Content-Type:application/json -x POST --data product[name]=nw&product[handle]=142&product[interval_unit]=day&product[interval]=1&product[price_in_cents]=0&product[initial_charge_in_cents]=14400&product[return_url]=http:\/\/54.145.218.63\/dev_lpad\/launchpad\/advertisers\/adcampaign\/56cee935-185c-4349-a8a1-2b6b0ae84a4d&product[return_params]={id={subscription_id}&customer_id={customer_id}} http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
Then i got the error.
Error: Unable to parse request body
Is there any way to solve this?
UPDATE : The URL provided here is a dummy value,Actually i am trying to connect with Chargify API (Recurring billing solution ).
It seems that your server does accept json payload post data. You probably forgot to json_decode your data, here is the fix:
curl -u sdfkjas2kjsd:x -H Accept:application/json -H Content-Type:application/json --data '{"product":{"name":"nw","handle":150,"interval_unit":"day","interval":1,"price_in_cents":0,"initial_charge_in_cents":14200,"return_url":"http:\/\/mytrial.com\/office\/selfie\/themes\/adcampaign\/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d","return_params":"id={subscription_id}&customer_id={customer_id})"}}' http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
If i send it to my php script <?php var_dump(json_decode(file_get_contents('php://input'))); I see correct answer:
object(stdClass)#1 (1) {
["product"]=>
object(stdClass)#2 (8) {
["name"]=> string(2) "nw"
["handle"]=> int(150)
...
Finally I could solve this issue by splitting the array parameters. My cURL cummand is this.
curl -u sdfkjas2kjsd:x -d 'product[name]":nw' -d '[product[handle]]=161' -d '[product[interval_unit]]=day' -d '[product[interval]]=1' -d '[product[price_in_cents]]=0' -d '[product[initial_charge_in_cents]]=14200' -d '[product[return_url]]=http:\/\/mytrial.com\/office\/selfie\/themes\/adcampaign\/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d' -d 'product[return_params]=id={subscription_id}&{customer_id={customerC_id}})' http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
I think you should put your data inside single quotes
curl ... --data 'some data here' ...
EDIT:
ON WINDOWS the proper way to pass array argument via cURL is shown below:
curl -X POST http://localhost:8080/uploadMultipleFiles -H "content-type: multipart/form-data" -F "files=#C:\Users\...\Desktop\filename1.txt,C:\Users\...\Desktop\filename2.txt,C:\Users\...\Desktop\filename3.txt,C:\Users\...\Desktop\filename4.txt
See the use of comma separated filenames where the server expect files to be an Array of Files.

Troubles "porting" cURL from bash to PHP

I'm trying to port this command to PHP:
curl -i -X POST http://website.com \
-H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
-H "Accept: Application/json" \
-H "X-Requested-With: XMLHttpRequest" --data "var1=output1&var2=output2"
From bash it works.. I get this JSON output.
This is what I wrote in PHP to try to get the same result:
<?php
function blabla() {
$curl_parameters = array(
'var1' => "output1",
'var2' => "output2",
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://website.com");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query( $curl_parameters ));
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
"Accept" => "Application/json",
"X-Requested-With" => "XMLHttpRequest",
));
$output=curl_exec($ch);
curl_close($ch);
}
echo blabla();
?>
Unfortunately with this snippet I just get a 302 Found HTTP header as output (this).. seems like the variables are not passed (the --data part from the bash command).
The problem is with your CURLOPT_HTTPHEADER. It should be an array of strings like this:
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
'Accept: application/json',
'X-Requested-With: XMLHttpRequest',
));
How about just curl_setopt($ch,CURLOPT_POSTFIELDS, $curl_parameters);? I don't think you need to call http_build_query.
Try add curl follow 302 redirect
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Also make sure your http_build_query() function uses "&" as argument separator. On some PHP configurations it may be "& amp;" by default
$query = http_build_query($data, '', '&');

Categories