hello i'm using code used by other people who supposedly have gotten it to work and have gotten their token information retrieved. The code is as follows:
$ch = curl_init();
$clientId = "myclientid";
$secret = "mysecret";
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json->access_token);
}
curl_close($ch); //THIS CODE IS NOW WORKING!
I retrieved this code from Paypal API with PHP and cURL and have seen it implemented in some others peoples code so i assume that it works. However i'm only receiving no response even though i am supplying the correct client id and secret (maybe a recent update has broken this code?).
The guide provided by Paypal on getting the access token is found here-> https://developer.paypal.com/docs/integration/direct/make-your-first-call/
however it demonstrates the solution in cURL and not through the PHP cURL extension so its alittle cryptic to me. Any help?
Well it seems that it is now a requirement to declare what type of SSL Version to use, thus the code above will work when curl_setopt($ch, CURLOPT_SSLVERSION , 6); //tlsv1.2 is inserted.
Just tested the API and I can confirm this code is working :
(No SSL options)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Language: en_US"
),
));
$result= curl_exec($curl);
$array=json_decode($result, true);
$token=$array['access_token'];
echo "<pre>";
print_r($array);
Related
To secure our passwords and API keys used on our website we use Microsoft Keyvault on Azure. Every time a user visits our website I make a connection to Keyvault and get the passwords and API keys. This means that every refresh of one of our pages of our website executes the following PHP code. This works perfectly and fast, but I would like to reduce the amount of logins to Microsoft Keyvault.
How can I check if the token is still valid for this user without saving the token in a session.
// GET TOKEN
$url = "https://login.microsoftonline.com/GUID/oauth2/v2.0/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "grant_type=client_credentials&client_id=GUID&client_secret=CLIENTSECRET&scope=https://vault.azure.net/.default";
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
$response = json_decode($response, true);
$token = $response['access_token'];
// LOGIN TO KEYVAULT WITH TOKEN
curl_setopt_array($curl, array(
CURLOPT_URL => "https://kv-webservice-westus.vault.azure.net/secrets/".$keyvault_secret."?api-version=7.2",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 25,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$token.''
),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
$keyvault_secret = $response['value'];
I recently got a new job. They need me to get information from ShipStation through the API using PHP. I'm fairly new to PHP and even newer to ShipStation. I copied the code from the API documentation and attempted to add the code for authorization. This is what I've got:
<?php
$apiKey = "my_api_key";
$apiSecret = "my_api_secret";
$auth = base64_encode($apiKey . ":" . $apiSecret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ssapi.shipstation.com/orders");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic " . $auth
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
Instead of giving me order information it's just returning bool(false).
I guess I'm just not seeing what I'm doing wrong. Any help would be greatly appreciated. Thanks!
This is coming up in searches for the query "Shipstation API PHP" so I wanted to point to this answer How do I make a request using HTTP basic authentication with PHP curl? and reiterate that it's a basic auth so you just need to do
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":" . $apiSecret);
I'd also recommend sending it over a secure connection to avoid man in the middle reading your API credentials.
I connect with CURL this way:
$url = 'https://ssapi.shipstation.com/orders';
$ShpStnAuth = 'Authorization: basic '.base64_encode('key:pair');
$curl = curl_init();
curl_setopt_array(
$curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Host: ssapi.shipstation.com',
$this->ShpStnAuth,
),
)
);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
I'm using PHP and cURL.
I want to send HTTP GET request to REST API and I want to call method from REST API. How can i make via cURL using PHP? I have API URL and Key. I must send my key with URL and I need method inside REST API. What I need to do? At the same time, i have to send basic authentication on all requests and I'll get JSON data.How can i do?
Thanks.
Regards
this is my code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$result = curl_exec($ch);
curl_close($ch);
I have tried your code and changed variables into array.
Added httpheaders, useragents.
And save to cookies and cookie location.
And tested:
$username = "username";
$password = "password";
$url = "http://localhost/html/login";
$send = "?username=".$username."&password=".$password;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0',
CURLOPT_POST => true,
CURLOPT_USERNAME, $username,
CURLOPT_USERPWD, $password,
CURLOPT_POSTFIELDS => $send,
CURLOPT_SSL_VERIFYPEER, false,
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array(
"Accept-Language: en-US;q=0.6,en;q=0.4",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Connection: keep-alive"
),
CURLOPT_COOKIEFILE => "cookies.txt",
CURLOPT_COOKIEJAR => "cookies.txt",
CURLOPT_REFERER => "http://localhost/html/login",
CURLOPT_ENCODING => 'gzip,deflate'
)); //gzip, if modul on
curl_exec($ch);
curl_close($ch);
I want to fetch content from my Atlassian with username and password.
The URL typically looks like:
http://my-own-site.atlassian.net/wiki/pages/viewpage.action?spaceKey=TO&title=Any-Wiki-Title
Is it possible to use PHP CURL to fetch content from this page?
So far I am only getting 401 auth reqd error.
I have looked through Stackoverflow and all I am getting is how to access basic atlassian.com and bitbucket.org pages.
With this code in php, you can create Confluence Pages:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"**inserttitle**\",\"space\":{\"key\":\"**insertspace**\"},\"ancestors\":[{\"type\":\"page\",\"id\":**insertancestor**}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
And with this code, you can get content from Confluence:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/**insertid**");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
echo $result;
Modify the parameter. I marked the words with insert*
Yes, it is certainly possible to access Atlassian products using PHP and cURL. I do it all the time to create/modify Jira issues
You will have to find/write a library (or set of libraries) which will allow you to access REST API calls. In my case, I wrote a base REST library which can then be inherited to create Jira, Confluence, any other REST service libraries
Search the Atlassian documentation site to find the REST API for the product you're using (Confluence in your case I would guess)
Don't forget that the REST API uses GET, POST, PUT and DELETE methods so your library will need to handle all of these
With regards to your error, I *think* your login will need to be allowed access to the API calls
To retrieve any existing content properties for a piece of content, use url as https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}
$curl = curl_init();
$post = array(
"id" => "{content_ID}",
"type" => "{content_ID}", //Ex. page
"title" => "{content_Title}",
"space" => ["key" => "{spaces_key}"],
"body" => ["storage" => ["value" => "<p>Here comes the other text</p>", "representation" => "storage"]],
"version" => ["number" => 15]
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}?expand=metadata.properties.myprop,space,body.view,version,container",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 300,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode($post),
CURLOPT_HTTPHEADER => array(
"authorization: Basic {base64 of (username:password)}",
"content-type: application/json",
'Accept: application/json'
),
));
$result = curl_exec($curl);
curl_close($curl);
I have been writing a RESTful API for my Laravel application.
I can submit data using cURL in Terminal with the following line;
curl -i --user admin#admin.com:qwertyuiop -d "data=somedata" https://www.xxxxxxxxxxxxx.co.uk/app/api/v1/clients
but when i try and do the same in cURL using PHP it gives me a 403 forbidden error.
$url = "https://www.xxxxxxxxxx.co.uk/app/api/v1/clients";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "admin#admin.com:qwertyuiop");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=mydata");
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
Any ideas what i'm missing from my PHP cURL script?
Thanks
It's been almost 6 years since this question asked. But I'm sure this answer will solve your and probably another problem with this. This code also solved my problem.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requested Headers
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
dd($response); //this line is my custom code
}
As explained in this amazing website, where I found this code about
Laravel POST using cURL