Curl function to get zone from pointhq.com - php

curl -u "user#example.com:f9eca287-5346-1cc7-8529-68eb9db7cd5e" -H 'Accept: application/json' -H "Content-type: application/json" https://pointhq.com/zones
How can I call this in php

I assume you are using basic authentication and you can use following;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pointhq.com/zones");
curl_setopt($s,CURLOPT_HTTPHEADER,array('Accept: application/json', 'Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "user#example.com:f9eca287-5346-1cc7-8529-68eb9db7cd5e");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
For more detail refer here

Related

Equivalent curl request in php

Im trying to get curl -X GET --header "Accept: application/json" --header "authorization: Bearer <API token>" "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2"in php
so far i came up with this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: Api key'
));
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
this is the 1st time im using curl in php, thanks in advance :)
Hey thank you all i got it work. i had to use
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Your are overwriting the header with the second set of the options. I would build up the header array like so:
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "authorization: Bearer " . $yourAPItoken;
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
And send the array:
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Crowd RESTful API cannot connect

I'm trying to use Crowd authentication into my website but I keep getting 401 Application failed to authenticate. I don't know why because I am passing in the application username and password correctly. What I have:
$data = array("value" => "APP_PASS");
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://PATH:8095/crowd/rest/usermanagement/2/authentication?username=APP_NAME');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Accept: application/json'
)
);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
You need to use CURLOPT_HTTPAUTH and CURLOPT_USERPWD for your application user:password. Crowd uses ACLs for the requesting application as well as the user authentication you're requesting.
curl -i -u 'APP_USER:APP_PASS' --data '{"value": "USER_PASS"}' https://SERVER_URL/crowd/rest/usermanagement/1/authentication?username=USER_ID --header 'Content-Type: application/json' --header 'Accept: application/json'

Trouble converting POST curl from command line to php

I am having trouble converting my curl command into php.
This part works great.
CURL command that adds an entry into my Parse.com database:
curl -X POST \
-H "X-Parse-Application-Id: my_id" \
-H "X-Parse-REST-API-Key: api_id" \
-H "Content-Type: application/json" \
-d "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}" \
https://api.parse.com/1/classes/MyClass
SOLVED ANSWER:
I have created this php script to replicate the command:
<?php
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array('X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);
?>
You've missed some crucial configurations.
These are the set the CURL to send request using POST, and the second is data to send. (RAW DATA is being sent as string into POSTFIELDS, those if you send array - it will automatically append header "multipart/form-data"
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);
HTH:)
Since you are doing a POST request you need to tell Curl to do that as well:
$postData = '{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
You may also need to supply a Content-Length header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Parse-Application-Id: my_id',
'X-Parse-REST-API-Key: api_id',
'Content-Type: application/json',
'Content-Length: '.strlen($postData))
);

Trying to use PayPal's api and need help converting Curl to PHP

This is PayPal's basic sandbox request to get an access token.
$curl = "curl -v https://api.sandbox.paypal.com/v1/oauth2/token
-H 'Accept: application/json' \
-H 'Accept-Language: en_US' \
-u 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp'
-d 'grant_type=client_credentials'";
This is what I've got so far as a conversion but it's failing.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Connection: Keep-Alive',
'Accept-Language: en_US',
'Accept: applictaion/json'
));
curl_setopt($ch, CURLOPT_USERPWD, 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
print 'Running curl';
$res = curl_exec($ch);
echo $res;
curl_close($ch);
Try removing
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Connection: Keep-Alive',
'Accept-Language: en_US',
'Accept: applictaion/json'
));
I copied and pasted your code and just removed the CURLOPT_HTTPHEADER option and it worked!

translating a command line curl into php

i am not very curl savvy was wondering if anyone could help me turn the following into php:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}' https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret
if you know of a good curl tutorial would also be great help.
something like this
$ch = curl_init();
$json = '{"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}';
$url = 'https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

Categories