Command cURL in PHP? - php

I'm using cURL cmd for trying a API.
I will need help to translate this into PHP :
curl -X POST "https://open.faceit.com/chat/v1/rooms/hub-4d2be024-1573-4312-9c56-425003027f08-general/messages" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <ACCES_TOKEN>" -d "{ \"body\": \"test\"}"
Thank you for your future help !

Something like this should do it, there are a lot of post on using cUrl with PHP
How to POST JSON Data With PHP cURL? , Set bearer token with cURL
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'https://open.faceit.com/chat/v1/rooms/hub-4d2be024-1573-4312-9c56-425003027f08-general/messages');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$authorization = "Authorization: Bearer ".$token;
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
# Return response instead of printing.
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
$post = json_encode(["body"=>"test"]);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)){
print "Nothing returned from url.<p>";
}
else{
print $buffer;
}
?>

Related

Example with parameter curl -x php

I have to make a curl request in php with these parameters, can you help me with an example, since I can't find anything similar
the link curl
curl -X GET "https://example.com/nms/api/v2.1/devices?withInterfaces=true&authorized=false&type=uisps&role=switch" -H "accept: application/json" -H "x-auth-token: wsedr4455-3345-es45-2345-4edeesssd"
You should transform it into PHP curl syntax. It should work like this:
$ch = curl_init('https://example.com/nms/api/v2.1/devices?withInterfaces=true&authorized=false&type=uisps&role=switch');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'accept: application/json',
'x-auth-token: wsedr4455-3345-es45-2345-4edeesssd'
));
$result = curl_exec($ch);
curl_close($ch);
The JSON incoming is in $result.

Output from curl is null

Hello i have never worked with curls in php before, i am trying to get the curl output that i also get when using it in the command line.
Here is what i have tried
$url = 'curl -X "GET" "https://api.spotify.com/v1/me/top/artists" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: MY KEY HERE"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
$result = file_get_contents($url);
var_dump(json_decode($result, true));
Those two var_dumps both get me the output NULL.
I am aware that my url is not correct, but i am not sure waht to change.

How to properly set headers for a json request through curl

I am new at API´s, Curl and Json. I need to link my appilcation with the an API.
The API I want to connect with has a CURL example which is this:
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X GET https://www.api.com -u 'user:token'
I have made some research and I make a request like this:
$url = "https://www.api.com";
$headers = array(
-H "Accept: application/json",
-H "Content-type: application/json"
-u 'Authorization: Basic '. base64_encode("user:token") // <---
);
//initializing curl object
$curl = curl_init();
//adding fields to the curl object to enter the site
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
//executing the curl call and getting data back
$json = curl_exec($curl);
curl_close($curl); // close the connection
//echo $json;
return $json;
I get the Json request but I still don´t understand the example the API gives. I even don´t know if I am setting well the Curl.
can someone explain to me how can I interpret the Curl example if I doing it well ?
This is not valid PHP syntax:
$headers = array(
-H "Accept: application/json",
-H "Content-type: application/json"
-u 'Authorization: Basic '. base64_encode("user:token") // <---
);
You should use this instead:
$headers = array(
'Accept: application/json',
'Content-type: application/json',
);
And for the auth, use this:
curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);

how Can i post data to an url using curl and json?

i want to use api of a website for send notification to my app
and the website help is not good :/
website said you should write in header of request tokan code and some setting
how can i post json data to this website with php ?
with these header
"Authorization: Token 7fb1………………………………29b464c "
"Content-Type: application/json"
"Accept: application/json"
and the url is from help text of website
curl -X POST "https://panel.pushe.co/api/v1/notifications/"
please help me#_#
Use something like this:
$ curl -X POST -i -H "Content-Type: application/json" -H "Authorization: TOKEN <YOUR_TOKEN>" https://panel.pushe.co/api/v1/notifications/
php example from here
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
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))
);
$result = curl_exec($ch);

How to use PHP CURL function to do the same request?

Here is my curl commaad line .
curl -u 'username:password' -X GET -H 'Accept: application/json' -H 'Content-Type: application/xml' -d '<request><layout>1</layout><filtermode>category</filtermode><filtervalue>115399046</filtervalue><limit>1</limit><start></start><sortfield></sortfield><sortdir></sortdir></request>' https://example.com/contacts
I use curl command line. It works for me. Now, I need do it in PHP.
The hard part is: The server accept GET request, but need a xml string in request body. I have tried to change GET to POST, the server did NOT return the correct message.
<?php
$ch = curl_init();
$credentials = "user:pass";
$data = 'xml string';
$page = "/contacts";
$headers = array( "GET ".$page." HTTP/1.0", "Content-type: application/xml", "Accept: application/json", "Authorization: Basic " . base64_encode($credentials) );
curl_setopt($ch, CURLOPT_URL, 'example.com/contacts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($ch);
?>
You can't put the GET line in $header, that causes an invalid header to be sent. The PHP equivalent of -X GET is:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

Categories