Cloudflare security level is not changing from api - php

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.

Related

How to correctly code Curl request in PHP with jsonrpc parameters?

I have a curl command that worls perfectly form the SSH shell, but cannot get it working with PHP Version 7.4.3.
I have tried several times using just about every example I can find on the Internet, all of them return a blank html page when run.
Here is the curl command (with user/pass and IP etc changed for obvious reasons);
curl \
--user someuser:password \
--data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' \
-H 'content-type: text/plain;' \
http://195.86.111.23:8223/
Can someone please educate me on how to turn this into something PHP can use?
I am currently testing with this code, but get an error "{"result":null,"error":{"code":-32600,"message":"Params must be an array"},"id":"curltest"} "
$payload=array();
$payload['jsonrpc']='1.0';
$payload['id']='curltest';
$payload['method']='getinfo';
$payload['params']='[]';
$payload=json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res=curl_exec($ch);
echo $res;
I have also tried using an automated tool here - https://incarnate.github.io/curl-to-php/ which seems to convert my curl command to PHP okay, but the page is empty on loading the provided code.
$payload = ["jsonrpc" => "1.0", id => "curltest", "method" => "getinfo", "params" => [] ];
Simple solution!

Razorpay x Api call BAD_REQUEST_ERROR using Laravel

I am trying to integrate razorpay x api for creating contacts and this is the sample api request data from razorpay. reference_id and notes are optional.
curl -u <YOUR_KEY>:<YOUR_SECRET> \
-X POST https://api.razorpay.com/v1/contacts \
-H "Content-Type: application/json" \
-d '{
"name":"Gaurav Kumar",
"email":"gaurav.kumar#example.com",
"contact":"9123456789",
"type":"employee",
"reference_id":"Acme Contact ID 12345",
"notes":{
"notes_key_1":"Tea, Earl Grey, Hot",
"notes_key_2":"Tea, Earl Grey… decaf."
}
}'
and below is my curl function data sent to razorpay.
$user=User::find($request->input('user_id'));
$payload= '{
"name":"'.$user->name.'",
"email":"'.$user->email.'",
"contact":"'.$user->mobile_number.'"
"type":"customer"
}';
$key="test_key";
$secret="secret_key";
$url = 'https://api.razorpay.com/v1/contacts';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_USERPWD, $key . ":" . $secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
I am getting response as name field is required, eve though I have sent all the request fields in the request parameter. Please suggest me if my curl function is correct or not. below is the response from api call
{
"error":
{
"code": "BAD_REQUEST_ERROR",
"description": "The name field is required.",
"metadata": {},
"field": "name"
}
}
Please specify the request method:
curl_setopt($ch, CURLOPT_POST, 1);

Want to manage mongoldb user on CodeIgniter using Parse Rest API

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/

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.

Categories