Ok, so I have been working on this app that fetches data from an API, i've been coding it in PHP, but I recently ran into an Error. I don't get any response
I got the script from an online blog, as I have no previous experience with cURL
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: xxxx',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
$get_data = callAPI('GET', 'https://api.brawlapi.cf/v1/player?tag=Y2QPGG', false);
$response = json_decode($get_data, true);
$errors = $response['response']['errors'];
$data = $response['response']['data'][0];
The API i'm using is https://docs.brawlapi.cf/#/endpoints/player
Related
I am making API call to third party via curl php.
the code is working but since I am making two POST and GET one after another I have to refresh the page to get response for GET call
I tried using ajax and it is throwing me CORS errors
function callAPI($method, $url, $data){
global $company_api_key;
switch ($method){
case "POST":
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '.base64_encode("$company_api_key") ,
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
break;
case "GET":
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '.base64_encode("$company_api_key") ,
'Content-Type: application/json',
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($ch);
if(!$result){die("Connection Failure");}
curl_close($ch);
return $result;
break;
default:
echo "something went wrong with API call";
}
}
$make_call = callAPI('POST', 'post-url', json_encode($data_array));
I get an ID after this call and then I use that id in GET call
$get_data = callAPI('GET', 'get-url'.$id, false);
now I am calling them one after another so I tried using AJAX for GET call but I am getting CORS error.
I am trying to connect to the Aliseeks api with CURL but I am not sure where to put my api key.
On the API's doc they say
"Aliseeks expects for the API key to be included in all API requests
in a header that look like the following: X-Api-Client-Id: Your Api
Key"
Now I am doing this :
$url = 'https://api.aliseeks.com/v1/products/details';
$curl = curl_init();
$auth_data = array(
"X-Api-Client-Id" => 'my_api_key_here',
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HEADER, $auth_data );
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;
But in the return I get :
[{"code":"no_token_found","violation":"aliseeks.authentication.notokenfound"}]
Any idea ?
ps: the $auth_data is here twice because I am trying kind of everything now...
You are using a wrong constant to send the headers.
It should be CURLOPT_HTTPHEADER instead of CURLOPT_HEADER.
I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
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);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?
I am in the process of creating a super simple PHP page that sends JSON data to an remote API via POST. I am having issues with it and hoping someone here could help me out.
This is what I have so far
$url = "https://remoteHost.com/api/post";
$json = '{"message":"textHere", "user":"userABC"}';
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic --ACB123-ABC123---ABC123---',
'Content-Type: application/json',
'Content-Length: ' . strlen($json),)
);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
When I execute this nothing happens. The page loads without errors but it looks like the curl isn't actually doing anything. Any Ideas?
I am trying to test client server with php and json. I am trying curl json request to an api url, and its seems it works.
Here is client code :
$data = array('id' => 2, 'something' => 4);
$content = json_encode($data);
$url = 'http://localhost/json.api.php';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
echo "<pre>";
var_dump($response);
curl_close($curl);
But, how can i get this request on server (on json.api.php) and parse it do something and then return json to requester client.
you can use $HTTP_RAW_POST_DATA to get the request