REST API Call via PHP cURL 500 server error - php

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?

Related

How to make two curl calls in same script

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.

Update data using PUT with CURL PHP

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?

cURL API integration with dynamic data and header authentication

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);

ARC extension return data but how to implement in php code

//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);
}
?>

Rest with Curl does not return anything

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;
?>

Categories