Equivalent curl request in php - 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);

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

How to send file for uploading using Php Curl Without Input type File Control?

I want to upload file from one server to another without using the file control and using Curl Request..
curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'api-token: 6a53bcbe222c490196e4b9f87ba9148c' --header 'api-secret: 6a53bcbe222c490196e4b9f87ba9148c' -F "document[]=#C:/xampp/htdocs/project/image_name.ext" -F "document[]=#C:/xampp/htdocs/project/image_name.ext" -F "document[]=#C:/xampp/htdocs/project/image_name.ext" 'https://server_api_url.com
'
The above curl works fine...
But i dont know how to post the file in php ...
Below is sample code how i am sending post data using php curl
<?php
$doc_file_path_string = ' -F document[]=#image_name';
array_push($doc_data_array, $doc_file_path_string);
$url = 'https://server_url.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode(' ',$doc_data_array));
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = "Accept: application/json";
$headers[] = "Api-Token: api_key";
$headers[] = "Api-Secret: api_key ";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo $result = curl_exec($ch);
?>
Here is how i did it
$url = <some url>;
$ch = curl_init();
$headers = ["Content-Type:multipart/form-data"];
// Define curl options
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// Here we make base64 encode for file content and push it into curl
$base64 = base64_encode(file_get_contents(<path to file>));
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $base64]);
// Make request
$response = curl_exec($ch);
curl_close($ch);

Curl function to get zone from pointhq.com

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

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

Execute SOAP using cURL

I am trying to execute an SOAP-function using cURL (because I get an error using the SoapClient().
This is my code (that is halfway working)
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$page = "/folder";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"customerSearch\"",
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
$data = curl_exec($ch);
The problem is that the SOAP-action is not being performed. And I also need to pass arguments to the action. Is this even possible?
You need to specify the cURL options to POST, and set the body of the request - if your not sending a body, there's no point in a POST request (and more importantly, it isn't SOAP). Building a complete HTTP request header just won't cut it.
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$body = ''; /// Your SOAP XML needs to be in this variable
$headers = array(
'Content-Type: text/xml; charset="utf-8"',
'Content-Length: '.strlen($body),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "customerSearch"'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
// Stuff I have added
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
$data = curl_exec($ch);

Categories