How to get an oauth_token with oAuth in PHP - php

I'm trying to authenticate using OAuth in OpenX (site does not render well in chrome. Use iexplore or safari.)
This is my piece of code
# Login
$url = "https://sso.openx.com/api/index/token";
$post = http_build_query( array( 'Access Token URL' => 'https://sso.openx.com/api/index/token',
'Authorize URL' => 'https://sso.openx.com/login/login',
'callbackUrl' => 'oob',
'Consumer Key' => $key,
'Consumer Secret' => $secret,
'OAuth Realm' => $realm,
'Request Token URL' => 'https://sso.openx.com/api/index/initiate',
'Signature Method' => 'HMAC-SHA1 ',
'Version' => '1.0a ') );
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
var_dump($json_response);
curl_close($curl);
$authObj = json_decode($json_response);
And, according to the linked documentation, I should be expecting an oauth_token and oauth_verifier:
1.Set the callbackUrl to oob (out-of-band), which tells the OAuth server that you are not redirecting a user. The OAuth Server returns the request token.
but instead I'm getting:
HTTP/1.1 400 Bad Request - Invalid Request: Missing parameters
Am I doing something obviously wrong here that I am missing? Am I misunderstanding something in the linked documentation?
Any sort of help is welcome, either aimed at the problem itself or to the way it's been presented; answers, hints, ideas, corrections, etc.
Thank you.

I am using openx too, here is my code. Hope it can help someone
$para = array (
'Access Token URL' => 'https://sso.openx.com/api/index/token',
'Authorize URL' => 'https://sso.openx.com/login/process',
'callbackUrl' => 'oob',
'Consumer Key' => $email,
'Consumer Secret' => $consumer_secret,
'OAuth Realm' => $sso_realm,
'Request Token URL' => 'https://sso.openx.com/api/index/initiate',
'Signature Method' => 'HMAC-SHA1',
'Version' => '1.0a'
);
$opt = array (
CURLOPT_URL => "https://sso.openx.com/login/process",
CURLOPT_COOKIEFILE => $cookieFile,
CURLOPT_COOKIEJAR => $cookieFile,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $para,
CURLOPT_VERBOSE => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => "{$authentication}",
CURLOPT_FOLLOWLOCATION => true
);
$c = curl_init();
curl_setopt_array($c, $opt);
$content = curl_exec($c);
$info = curl_getinfo($c);
$error = curl_error($c);
You need the "consumer_secret" and "sso_realm" from the email openx people sent to you.

Try passing the parameters like this
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
where $data will have $data='username='.$username.'&password='.$password.'';

Where does "$resource" come from?? Replace:
curl_setopt($resource, CURLOPT_POSTFIELDS, $post);
with
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
for a start.

Related

WHMCS api call "DomainWhois" response status =>"error" instead of "available/unavailable"

I want to check the availability of domain through API. Other api is working perfectly but the "DomainWhois" api is not working as expected.
I have tried the following code which was available at WHMCS.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(
array(
'action' => 'DomainWhois',
// See https://developers.whmcs.com/api/authentication
'username' => 'IDENTIFIER_OR_ADMIN_USERNAME',
'password' => 'SECRET_OR_HASHED_PASSWORD',
'domain' => 'example.com',
'responsetype' => 'json',
)
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);```
I have use the above code with my credentials but got below response for every domain check.
{"result" => "success" "status" => "error" "whois" => ""}

CURL Post request yields in error '{"error":"invalid_request"}' (length=27)

CURL Post request yields in error '{"error":"invalid_request"}' (length=27)
here is my code
$url="https://...................";
$authorization=""; //base64 string TE1TQXBpOkxNUContinue...=
$curl = curl_init();
$auth_data = array(
'scope' => 'LMSApi LMSRead',
'grant_type' => 'client_credentials'
);
$ops=array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS=>$auth_data,
CURLOPT_HTTPAUTH=> CURLAUTH_BASIC,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Content-Type'=> 'application/x-www-form-urlencode',
'Authorization'=> 'Basic '.$authorization
)
);
curl_setopt_array($curl,$ops);
$response = curl_exec($curl);
var_dump($response);
curl_close($curl);
when using postman it works but returns always false while using above code. don't know what I am doing wrong.curl_error($ch) results in empty string
help is required
if someone have the same problem, following is the solution.
passing 'grant_type=client_credentials&scope=specify scope' directly to CURLOPT_POSTFIELDS instead of an array solved my problem. the code now looks like as bellow.
$url="https://something.com";
$headerpost=array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic yourbase64string'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=LMSApi LMSRead');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headerpost);
$result = curl_exec($ch);
$jsonresult=json_decode($result,true);
curl_close($ch);

PHP curl and ISIC verification (REST api). How do I make this work?

I would like to verify whether someone has a valid ISIC card, I have written the following code for this Rest API (http://nakoduj.to/_upload/project_files/2015-08-18-12-51-20_DM%20-%20Integration%20Manual.pdf), but it doesn't work, and I have no idea why it doesn't.
$data = array( "cardNumber" => "S123456789000A",
"cardholderName" => "John Doe");
$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gts-dm.orchitech.net/api/verifications');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "testdm:testdm");
$result = curl_exec($ch);
if ($result === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
I'm getting false for the $result always. And there is no additional information in $info.
Thank you in advance for your help
<?php
$data = [
"cardNumber" => "S123456789000A",
"cardholderName" => "John Doe",
];
$data = json_encode($data);
$header = $request_headers = [
"Content-Type: application/json"
];
$curl_options = [
CURLOPT_URL =>'https://gts-dm.orchitech.net/api/verifications',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FILETIME => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_USERPWD => "testdm:testdm"
];
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
The request header was not sent causing http error code 415 UnsupportedMediaTypeHttpException Hence need to add request header, working fine
I recommend you to use Postman. Just install it and try to make request with it. You'll see the result of your request and than can simply get the request code out.
You can see 'Code' on your top-right corner below 'save', press it and select your language. For your purposes it is PHP->cURL. And the result will look like this:
More about Postman here

You must log in before using this part of Bugzilla, code:410

I am able to get the GET request working but having issues related to authentication in POST and PUT request. I am getting the error "You must log in before using this part of Bugzilla". I have provided the correct username and password. I have tried CURLAUTH_ANY as well as CURLAUTH_BASIC. I have tried both PUT and POST request. Any help is appreciated.
$url ="http://localhost:8080/bugzilla/rest/bug/2";
$apikey = "IZC4rs2gstCal0jEZosFjDBRV9AQv2gF0udh4hgq";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'abbincrc';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $result
Following code solved my problem. I have written a blog on it which might be useful to others encountering the same problem.
<?php
$url = 'http://localhost:8080//bugzilla/xmlrpc.cgi';
$ch = curl_init();
$header = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array( 'Content-Type: text/xml', 'charset=utf-8' )
);
curl_setopt_array($ch, $header);
$bugreport = array(
'login' => 'ashish.sureka#in.abb.com',
'password' => 'abbincrc',
'product' => "TestProduct",
'component' => "TestComponent",
'summary' => "Bug Title : A One Line Summary",
'assigned_to' => "ashish.sureka#in.abb.com",
'version' => "unspecified",
'description' => "Bug Description : A Detailed Problem Description",
'op_sys' => "All",
'platform' => "All",
'priority' => "Normal",
'severity' => "Trivial"
);
$request = xmlrpc_encode_request("Bug.create", $bugreport);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_exec($ch)
?>

How can I forward $_POST with PHP and cURL?

I receive a POST request at my PHP script and would like to forward this POST call to another script using POST too. How can I do this?
I can use cURL if it's required for this action.
Perhaps:
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
From curl_setopt:
This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.
Do this,
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($_POST));
Here's a fully functional cURL request that re-routes $_POST where you want (based on ZZ coder's reply)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://urlOfFileWherePostIsSubmitted.com");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// ZZ coder's part
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
$response = curl_exec($ch);
curl_close($ch);
<?php
function executeCurl($arrOptions) {
$mixCH = curl_init();
foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
}
$mixResponse = curl_exec($mixCH);
curl_close($mixCH);
return $mixResponse;
}
// If need any HTTP authentication
$username = 'http-auth-username';
$password = 'http-auth-password';
$requestType = 'POST'; // This can be PUT or POST
// This can be $arrPostData = $_POST;
$arrPostData = array(
'key1' => 'value-1-for-k1y-1',
'key2' => 'value-2-for-key-2',
'key3' => array(
'key31' => 'value-for-key-3-1',
'key32' => array(
'key321' => 'value-for-key321'
)
),
'key4' => array(
'key' => 'value'
)
);
// You can set your POST data
$postData = http_build_query($arrPostData); // Raw PHP array
$postData = json_encode($arrPostData); // ONLY use this when requesting JSON data
$arrResponse = executeCurl(array(
CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_VERBOSE => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CUSTOMREQUEST => $requestType,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"X-HTTP-Method-Override: " . $requestType,
'Content-Type: application/json', // ONLY use this when request json data
),
// If HTTP authentication is required , use the below lines
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username. ':' . $password
));

Categories