I want to call call the mifinity api using php - php

I am using the following api:http://apidocs.mifinity.com/#/doc/2 but its not working??
My API key
$data = array(
'key' => "1234567777"
);
$url = 'https://demo.mifinitypay.com/';
$ch = curl_init($url);
$postString = http_build_query($data, '', '&');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

$request = new HttpRequest();
$request->setUrl('https://demo.mifinitypay.com/api/oauth/token');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'postman-token' => '6396dfb7-540b-0e7d-7333-5d0eeff4d606',
'cache-control' => 'no-cache',
'authorization' => 'Basic username:password encoded using Base64',
'x-api-version' => '1',
'accept' => 'application/json',
'content-type' => 'application/x-www-form-urlencoded'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
'grant_type' => 'client_credentials'
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}

Related

How to re-write this cURL request in Guzzle 6?

Example Code (provided by Channel Advisor):
https://developer.channeladvisor.com/authorization/updating-access-token/php-updating-access-token
<?php
$endpoint = "https://api.channeladvisor.com/oauth2/token";
$refresh_token="{{REFRESH_TOKEN}}";
$application_id= "{{APPLICATION_ID}}";
$shared_secret="{{SHARED_SECRET}}";
$url = $endpoint;
$client_id = base64_encode("$application_id:$shared_secret");
$body = "grant_type=refresh_token&refresh_token=$refresh_token";
$length = strlen($body);
$headers = array(
"Authorization: Basic $client_id",
"Content-Type: text/plain",
"Cache-Control: no-cache",
"Content-Length: $length"
);
echo "URL:".$url."\n";
echo "Body:$body\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
print_r($httpcode);
echo"\n";
$json = json_decode($result, true);
var_dump($json);
?>
Attempting to rewrite as Guzzle HTTP request:
My question is am I setting the Basic authentication correctly?
$accessToken = {{ACCESS_TOKEN}};
$refresh_token = {{ACCESS_TOKEN}};
$application_id = {{APPLICATION_ID}};
$shared_secret = {{SHARED_SECRET}};
$base_uri = "https://api.channeladvisor.com/oauth2/token";
$client_id = 'Basic ' . base64_encode("$application_id:$shared_secret");
$client = new Client(
['headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $client_id
]
]
);
$result = $client->request('POST', $base_uri, [
'query' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token
],
]);
$body = $response->getBody();
// Implicitly cast the body to a string and echo it
echo $body;
You're sending POST request, then I guess you should send FORM data, not QUERY data.
$response = $client->request('POST', $base_uri, [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token
],
]);

how to send access token using CURL PHP

I am new in CURL I need to authenticate user is logged in or not using access token. But how to send x-access-token with post request into header.
I am using. curl library
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
'newPassword' => $_POST['newPassword'],
'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
$response = array(
'status' => $curl->errorCode,
'message' => $curl->errorMessage,
'response' => $curl->response
);
echo json_encode($response);
} else {
$response = array(
'status' => $curl->httpStatusCode,
'message' => 'Successfylly Login',
'response' => $curl->response
);
echo json_encode($response);
}
You can do this way also.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.abctest.com/abc.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//********************Check these lines for headers*******************
$headers = [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'x-access-token':'sdsdgdsggrtyrtghf'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//************************ END Headers *********************************
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
You can use setHeader method in Curl class.
RTFM plz.
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
// check the following line
$curl->setHeader('x-access-token', 'YOUR-TOKEN-HERE');
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
'newPassword' => $_POST['newPassword'],
'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
$response = array(
'status' => $curl->errorCode,
'message' => $curl->errorMessage,
'response' => $curl->response
);
echo json_encode($response);
} else {
$response = array(
'status' => $curl->httpStatusCode,
'message' => 'Successfylly Login',
'response' => $curl->response
);
echo json_encode($response);
}

Need help converting a PHP CURL script to guzzle

Hello I have this working CURL post request I wish to recode with guzzle. But I'm failing on a 400 Bad Request. Anyone see what I'm doing wrong:
$response = $this->getHttpClient()->post(
'https://login.eveonline.com/oauth/token', [
'headers' => [
'Authorization' => 'Basic '.base64_encode(
env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
)
],
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]);
return json_decode($response->getBody()->getContents(), true);
/*
$c = curl_init();
$cver = curl_version();
$contact = 'postmaster#asite.net';
curl_setopt($c, CURLOPT_URL, 'https://login.eveonline.com/oauth/token');
curl_setopt($c, CURLOPT_USERAGENT, "asite/admin (curl/{$cver['version']}; {$contact})");
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]));
curl_setopt($c, CURLOPT_HTTPHEADER, [ 'Authorization: Basic '.base64_encode(
env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
) ]);
$rawjson = curl_exec($c);
return json_decode($rawjson, true);
*/
RESPONSE:
Client error: POST https://login.eveonline.com/oauth/token resulted in a 400 Bad Request response:
{"error":"invalid_request","error_description":"Unknown grant_type"}
Fixed it already sorry for the inconvenience:
$response = $this->getHttpClient()->post(
'https://login.eveonline.com/oauth/token', [
'headers' => [
'Authorization' => 'Basic '.base64_encode(
env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
)
],
'json' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]
]);

Api working on curl and not on GuzzleHttp

I'm trying to connect to the Talentlink Api. I'm able to do it on curl but I can't connect using GuzzleHttp. I'm using Guzzle through the m6web/guzzle-http-bundle on Symfony. My code is below. Does anybody have an idea?
CURL
$headers = [
'username: XXXXX',
'password: XXXXX'
];
$body = '{
"searchCriteriaSorting": {
"categoryListsSorting": "LABEL",
"customLovsSorting": "ORDER",
"standardLovsSorting": "ORDER"
}
}';
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://api3.lumesse-talenthub.com/CareerPortal/REST/FoAdvert/advertisement-by-id?api_key=XXXXXX&lang=FR&postingTargetId=1");
curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);
curl_setopt($tuCurl, CURLOPT_HEADER, 1);
curl_setopt($tuCurl, CURLINFO_HEADER_OUT, 1);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, $headers);
$head = curl_exec($tuCurl);
$httpCode = curl_getinfo($tuCurl, CURLINFO_HTTP_CODE);
curl_close($tuCurl);
GUZZLE
$headers = [
'username' => 'XXXXXX',
'password' => 'XXXXXX'
];
try {
$response = $client->request('GET',
'https://api3.lumesse-talenthub.com/CareerPortal/REST/FoAdvert/advertisement-by-id?api_key=XXXXX&lang=FR&postingTargetId=1',
array(
'debug' => true,
$headers
));
} catch (ClientException $e) {
die((string)$e->getResponse()->getBody()->getContents());
}
On Guzzle, I keep getting a page with a login form as if I wasn't connected. However, the status is always 200 so it's difficult to debug.
SOLUTION
It was a problem with the array I was sending. It should be like this wit the 'header' key:
$response = $client->request('GET',
'https://api3.lumesse-talenthub.com/CareerPortal/REST/FoAdvert/advertisement-by-id?api_key=XXXX&lang=FR&postingTargetId=1',
[
'headers' => $headers,
//'debug' => true
]);

cURL request Spotify API Unexpected status: 415

Here is my script for an auth request to Spotify but it returns an error. I tried changing the Content-Type, but that doesn't seem to cut it. Here is my code:
$spot_api_client = 'client';
$spot_api_secret = 'secret';
$spot_api_redirect = 'myurl';
if(isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']){
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://accounts.spotify.com/api/token",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => urlencode($spot_api_redirect),
),
CURLOPT_HTTPHEADER => array(
'Accept' => '*/*',
'User-Agent' => 'runscope/0.1',
'Authorization' => 'Basic '. base64_encode($spot_api_client.':'.$spot_api_secret),
'Content-Type'=>'application/json'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
}
Well, seems I found the answer based on this question :
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$spot_api_redirect = 'myurl';
$credentials = "client:secret";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"User-Agent: runscope/0.1",
"Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=authorization_code&code='.$_GET['code'].'&redirect_uri='.urlencode($spot_api_redirect);
if(isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']){
unset($_COOKIE['stateKey']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($response);
}
Just in case of node.js / request use, point out "WTF" tag :)
request(
{
method : "POST",
url : "https://accounts.spotify.com/api/token",
json : true,
headers :
{
"Content-Type" : "application/x-www-form-urlencoded", // WTF!
"Authorization" : "Basic " + new Buffer(client_id+":"+client_secret).toString('base64')
},
body : "grant_type=client_credentials"
},
function (err, response, body)
{
if (err)
{
cb(err);
}
else
{
if (body.hasOwnProperty("access_token"))
{
access_token = body.access_token;
cb(null);
}
else
{
cb(body);
}
}
}
);

Categories