Example with parameter curl -x php - 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.

Related

Command cURL in 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;
}
?>

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 to do this curl request via php / laravel?

I have a curl request that was given to me as part of an API documentation, and can't for the life of me get it to work via php (in Laravel).
The documentation says to do this...
wget -O- \
--header 'x-user:x' \
--header 'x-password:y' \
'http://example.com'
What I'm trying is this...
$url = 'http://example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "test_user:test_password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
dd($info);
This is just echoing out Authentication Failed and then a bunch of info on my request, but not actual data.
Can anyone point out what I'm doing wrong?
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'x-user: something', 'x-password: something' ));

How to pass this cURL in PHP?

I got this from the IBM Watson speech to text documents, to send an audio file for processing:
curl -X POST -u <username>:<password>
--header "Content-Type: audio/flac"
--header "Transfer-Encoding: chunked"
--data-binary #<path>0001.flac
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true"
But how should I build that into PHP cURL? Something like this?
$headr = array();
$headr[] = 'Content-Type: audio/flac';
$headr[] = 'Transfer-Encoding: chunked';
...
$crl = curl_init('https://stream.watsonplatform.net/speech-to-text/api');
curl_setopt($crl, CURLOPT_POST, 1);
curl_setopt($crl, CURLOPT_POSTFIELDS, array("username:password"=>"myuser:mypassword","data-binary"=>"#<path>0001.flac"));
curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);
What about the -x and -u?
-X POST is already set when you did this
curl_setopt($crl, CURLOPT_POST, 1);
as for -u, you can use this, don't include the username:password in your post field array.
curl_setopt($crl, CURLOPT_USERPWD, "yourusername:yourpassword");
for using --data-binary, there is already an existing stackoverflow post on how to accomplish it using php curl: data-binary parameter in cURL

Categories