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.
Related
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?
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
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 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 want to login using JIRA API with curl operation.
I having problem in curl operation. I have main URL in JIRA_URL.'/demo/111'; values for $username and $password are passed in the function correctly but shows the status 'failure'. Is any issues in my curl code
function JIRA_authenticate($username, $password) {
$url = JIRA_URL . '/demo/111';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // ssl ensure cert
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); /// ssl ensure cert
$issue_list = (curl_exec($curl));
echo $issue_list;
return $issue_list;
}
You didn't mention what you are trying to achieve with this code.
Are you trying to get a ticket info, post an issue? You are just using the API...
Well here's a script that works with the JIRA API.
<?php
$username = 'test';
$password = 'test';
$url = "https://xxxxx.xxxxxxx.net/rest/api/2/project";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$test = "This is the content of the custom field.";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);
?>
This code is fetching a project from JIRA.
If you want to create issues, you will have to change the REST URL to /rest/api/2/issue/ and use "POST" instead of "GET" method.