Getting Data from API - PHP - php

I have been trying to connect to an API, which requires both a user and password key. I was able to connect using curl from the command line, however am having trouble using PHP.
I have tried using "user:password" as the AUTH key (which worked in curl), previously the code simply required a password and worked.
Below is the code that I am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "api-link-removed.com" . urlencode($location),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Basic " . AUTH_KEY,
"content-type: application/json"
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
Any suggestions to what I might be doing wrong would be much appreciated!
Thanks in advance!

Maybe Guzzle HTTP
$client = new GuzzleHttp\Client();
$response = $client->get('http://www.server.com/endpoint', [
'auth' => [
'username',
'password'
]
]);
https://docs.guzzlephp.org/en/stable/

I tried this:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt_array($curl, [
CURLOPT_URL => "your api url here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Name of api host: the name here",
"Api key : api key here"
]
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Related

Apply value into cURL on PHP

I have a question about curl.
Here is the value which will be changed by the user input.
$uid = "xxxxx" //Get from system
$token = "xxxxx" //Get from system
$title = "Testing Message"; //user input
$body = "Message"; //user input
$url_on_click = "https://www.xxxxx.com"; //user input
I want to apply the value into the function but it getting error:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"uid" :' . $record_uid . ',
"payload": ' . $payload . '
}',
CURLOPT_HTTPHEADER => array(
'token: ' . $record_token . ',
Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
If I change all the value to string. It's work. The follow code like :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{"uid" :"xxxxxx",
"payload": {
"title": "Testing Message",
"body": "1234",
"icon":"https://xxxxx.net/xxxxx.png",
"urlOnClick":"xxxxx.com"
}}',
CURLOPT_HTTPHEADER => array(
'token: xxxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
May I know which part I did wrong? That issue really pull my hair out.
Thank you =]
Try this, post fields depend on your CURLOPT_HTTPHEADER, according to your header you need to convert it in JSON otherwise, the array type will work
$postFields = array(
'uid' => "xxxx",
'token' => "xxxx",
'title' => "xxxx",
'body' => "xxxx",
'url_on_click' => "xxxx"
);
//curl init lines
//-------
//-------
CURLOPT_POSTFIELDS => json_encode($postFields),

Getting a response through Curl URL in PHP

I am trying to perform some function based on the response I will get from Curl URL. Below is the code I have written. But the response coming is always empty. Due to which the control always goes into an else condition.
if(isset($_POST['verify_button'])){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://*******/v1/****/*********/".$_POST['********']."?MVNO=*******",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic Z*******",
"accept: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response);
if($data->resultCode == '0'){
echo '<div class="mm_success_alert">Sucess</div>';
}
else{
echo '<div class="mm_danger_alert">Fail.</div>';
}

How to generate dynamic authorization code using JWT api, user and password

Right now i am able to get the data from api by using below code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://test-api.test.org/user/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJ0eXAiOiJKV1QiI1NiJ9.eyJhdWQiOiIxIiwianRpIjoN2ZiQwiZXhwIjoxNTk0MjY5NjI"
),
));
$response = curl_exec($curl);
$data = json_decode($response, true);
echo $response;
But authorization code is expiring every 15 min and i have to refresh the token after that interval in php.
My requirement is:
To generate token dynamically by using jwt api, user and password
check for if the token is valid or expired after sometime
once the token is expired then refresh it and generate a new token dynamically.
Thanks in advance for all your support.
I found solution of my problem and its working perfectly fine.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://{instance}/app/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"username\":\"yourusername\", \"yourpassword\":\"{pass}\"}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Passing form variable into php curl request

I've searched the site extensively and I just can't get the syntax correct in my attempt to replace a parameter in a Curl request with data inserted from a form.
This code works:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8000",
CURLOPT_URL => "http://nd-337-495-552.rg-709-313.p2pify.com:8000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\"method\":\"liststreamitems\",\"params\":[\"Device\"],\"chain_name\":\"nw-504-042-6\"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefghijklmnop1234567=",
"Content-Type: application/json",
"Postman-Token: dd629e44-26fb-4ed1-ac4e-c47faf38c356",
"cache-control: no-cache"
), // auth header content deliberately changed
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
This code, where I try to replace "nw-504-042-6\" with the variable $ChainName in CURLOPT_POSTFIELDS does not work.
<?php
$ChainName = $_POST['ChainName'];
$StreamName = $_POST['StreamName'];
echo $ChainName;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8000",
CURLOPT_URL => "http://nd-337-495-552.rg-709-313.p2pify.com:8000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\"method\":\"liststreamitems\",
\"params\":[\"Device\"],
\"chain_name\":"'"$ChainName"'"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefghijklmnop1234567=",
"Postman-Token: 0bcbbec4-cc25-4059-b2c4-220c4722b9d5",
"cache-control: no-cache"
), // auth header content deliberately changed
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Would appreciate the assistance
Try to json_encode your params
$params = [
"method"=>"liststreamitems",
"params"=>["Device"],
"chain_name"=>$ChainName
];
Then pass it to the option CURLOPT_POSTFIELDS like this
CURLOPT_POSTFIELDS => json_encode($params);

No response from curl operation but success in postman

The below one i tested with postman its returning the response but when i check in the PHP its not returning any response.please help me.
cURL file creation:
$curl_file = curl_file_create(\Yii::$app->basePath.'/web/uploads/'.$uploadedFile->name,'pbix',$uploadedFile->baseName);
$params = ['file' => $curl_file];
cURL Process:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://***.***.**.***/v1.0/collections/**/workspaces/**/***?datasetDisplayName=new test",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => array(
"authorization: key",
),
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
The problem is space in the parameter.The space should be replaced by %20. you need to do like below:
$url="https://***.***.**.***/v1.0/collections/**/workspaces/3b09eff6-69ae-4932-9419-14eeb69a9dbd/***?datasetDisplayName=new test";
$curl_url=str_replace(" ","%20",$url);
or you have to use urlencode.This is used when encoding a string to be used in a query part of a URL.
$curl_url="https://***.***.**.***/v1.0/collections/**/workspaces/3b09eff6-69ae-4932-9419-14eeb69a9dbd/***?datasetDisplayName=urlencode(new test)";
Then
curl_setopt_array($curl, array(
CURLOPT_URL => $curl_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => array(
"authorization: key",
),
));

Categories