This is the code.
I am using a rest api to and http authentication to authenticate the user.
On running the function, nothing is getting displayed.
I am not getting a success/error from the response. The response is blank and there is no error as well. Please help
<?php
$method = "POST";
$url = "https://www.myproject.com/api/v1/projects/create";
$data_array = array("projectName"=>"Project1");
$data = json_encode($data_array);
$response = CallAPI($method,$url,$data);
function CallAPI($method, $url, $data)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//base64
curl_setopt($curl, CURLOPT_USERPWD, "anemail#gmail.com:arpit.1995");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$response = CallAPI($method,$url,$data);
echo $response;
?>
Related
here is my code below ,i need to get the value of account_name from the beow API request, kindly go through it and advise. The only value i need to get is account_name value. Kindly help out thanks
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"authorization: Bearer sk_test_df78c3c2d08ea1f266b8b19524a249e77c0eaa14",
"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;
}
$bank="033";
$accno=2056194452;
$get_data = callAPI('GET', 'https://api.paystack.co/bank/resolve?account_number='.$accno.'&bank_code='.urlencode($bank), false);
$response = json_decode(json_encode($get_data), true);
echo $response;````
#Result of code will output the below:#
`{ "status": true, "message": "Account number resolved", "data": { "account_number": "2056194452", "account_name": "OLADEPO WISDOM IBRAHIM", "bank_id": 18 } }`
The API returns JSON data, so there is not reason encode it.
If you use json_decode and set the "assoc" parameter to true (like you already have), it should return an associative array. You can then get the "account_name" variable from the array.
$get_data = callAPI('GET', 'https://api.paystack.co/bank/resolve?account_number='.$accno.'&bank_code='.urlencode($bank), false);
$response = json_decode($get_data, true);
echo $response['data']['account_name'];
I have the following code:
function callAPI($method, $url, $data, $auth){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '.$auth,
'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;
}
$data = ""; // some json data
$UserID = 'x';
$Password = 'y!';
$auth = base64_encode($UserID.':'.$Password);
callAPI('POST','https://collaudo-wsrest.sda.it/SPEDIZIONE-WS-WEB/rest/spedizioneService',$data, $auth);
When executed, it returns 'connection error' (because I setup it), but specifically 500 server error.
The fact is, if I run my call here: https://reqbin.com/ it works perfectly. What's wrong in my code?
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 working on auto sync between two systems. We have the database in one system and we are using another system for marketing. I am able to pull complete data from the database system. But when I am inserting the data to the marketing tool, it says invalid body request.
in the documentation, these are the details:
1)-url-:
https://api.customdomain/recipients
2)-request body:
[
{
"email": "jones#example.com",
"last_name": "Jones",
}
]
3)-auth-headers:
authorization is done through the header.
Authorization: Bearer *API key goes here*;
This is the code I tried. I should able to place data dynamically.
$fields = array(
'email' => "jam22#example.com",
'last_name' => "test40"
);
$methd="POST";
$url="https://api.customdomain/recipients";
$data=http_build_query($fields);
$rep= CallAPI($methd, $url, $fields);
echo $rep;
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
$headr=array(
"authorization: Bearer *API key here*",
"cache-control: no-cache"
);
$ver="CURL_HTTP_VERSION_1_1";
// Optional Authentication:
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($curl, CURLOPT_HTTP_VERSION,$ver);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
I got it. The format of the body which Marketing tool receives is different from this array method. I modified the array to a string. Now it is working perfectly.
This is enough from CURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.custom");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//Setting post data as xml
curl_setopt($curl, CURLOPT_HTTPHEADER, array("authorization: API key"));
$result = curl_exec($curl);
curl_close($curl);
print($result);
//I have used this code but returning blank and what data to pass in $data array.I share you all detail here.
http://dev.hordanso.com:5050
merchant=justenergy&username=mogobahor&password=VerJust123&method=GetHistory
$method='POST';
$url='http://dev.hordanso.com:5050';
$data=array("headers"=>"GetHistory","Content-Type"=>"application/x-www-form-urlencoded","name"=>"JustEnergyAPI-AlphaBills","payload"=>"merchant=justenergy&method=GetNextPayment","kind"=>"ARC#RequestData");
CallAPI($method, $url, $data);
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "mogobahor:VerJust123");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
}
?>