Converting cli curl to php url - php

I have problem with converting cli cURL to php cURL version.
I've tested such link from cli:
curl -H "X-API-KEY: key" -XPOST -v -H "Accept: application/json" http://sentione.com/api/statements/search -H "Content-Type: application/json" -d '{"topicId":id, "from": "2014-01-01 00:00:00.000 CET"}'
and it's working properly.
And that's my code.
I got no response.
$headers = array(
"Content-type: application/json",
"X-API-KEY: key",
"Accept: application/json"
);
$arr = array(
"topicId" => id,
"from" => "2014-01-01 00:00:00.000 CET"
);
$url = "http://sentione.com/api/statements/search";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arr));
$response = curl_exec($curl);
print_r($response);
Of course I set correct api key and topic id.
What am I doing wrong?

My bad, see at my array declaration.
I used : instead of =>. I don't know why.
It's solved, thanks anyway! :)

Related

How to cURL to PHP Curl PayQuicker

How can I transfer this cURL into php? It's not returning anything
Trying all the ways and not working. Do you know a library for codeigniter that not requires composer?
curl --location --request POST 'https://identity.mypayquicker.build/core/connect/token' \\\
--header 'Authorization: Basic {token}'
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=modify'
--data-urlencode 'scope=readonly'",
"language": "curl",
"name": " "
Doing this way
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Basic {token}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://identity.mypayquicker.com/core/connect/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, TRUE);
// This option is set to TRUE so that the response
// doesnot get printed and is stored directly in
// the variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'grant_type' => 'client_credentials'
));
$data = curl_exec($ch);
var_dump($data);
Not Returning Anything

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);

PHP curl_setopt equivalent to curl -d

I got the following curl command to work on Linux:
curl -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Basic dGVsZXVuZzpuYWcweWEyMw==" -X PUT -d '{"requireJiraIssue": true, "requireMatchingAuthorEmail": "true"}' http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc%3AyaccHook/enabled
However, when I tried to do this on PHP, the data is not being sent to the server properly, this is my set_opt commands:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));
What did I miss?
You use CURL options improperly. CURLOPT_PUT is intended for sending files, it is not suited for your case. You have to use CURLOPT_CUSTOMREQUEST option and set it to "PUT", not "POST".
So the code should look like this:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));

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))
);

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