AZURE - The official documentation and examples for PHP is not working, help?
I am using the code at this link:
https://github.com/courtney7/HTTP-Code-Samples/blob/37a4431f75397e1ccc6ee3f62ef14b3909a2dc85/PHP/PHPAzureToken.php
Always test and debug APIs using curl (i.e. take a curl first approach). Here's an example that works on my Windows 10 system. Here's the curl that gets a token.
curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Ocp-Apim-Subscription-Key>"
Example to get token and call text translate.
curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Ocp-Apim-Subscription-Key>" > Bearer.txt
set /p Bearer= < Bearer.txt
curl -H "Authorization: Bearer %Bearer%" "https://api.microsofttranslator.com/v2/Http.svc/Translate?Text=Hello+world.&From=en&To=es"
Make sure you are calling the correct endpoint "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
Related
I need help because in my team we are using an old version of php and its impossible to use any bundle.
I'm trying to make the request with curl but Im having a lot of problems with authentication.
Could anyone tell me how to make a simple curl request to send a tweet using the twitter api?
Thanks a lot!!
curl -X POST 'https://api.twitter.com/2/tweets/search/stream/rules' \
-H "Content-type: application/json" \
-H "Authorization: Bearer $APP_ACCESS_TOKEN" -d \
'{
"add": [
{"value": "cat has:images", "tag": "cats with images"}
]
}'
it's just an example - but it's nicely described in the documentation
I have been working with Jenkins, and now Im stuck when I try to make an HTTP Post using curl, I am sending a json file, to a file in my page who is waiting for it, and then do some functions, the code I am using is the following:
curl -X POST -k -i -H "Accept: application/json" -w "%{body}" -w "%{http_code}" -d "#/var/lib/jenkins/workspace/myFolder/session.json" http://mypage.com/myFolder/newfile.php
But apparently Im not sending correctly the data of the file, the newfile.php has something like this:
echo $_REQUEST['sessionId'];
But always get an error when I try to echo the index sessionId, I mean my file "newfile.php" is not getting anything from my jenkins curl, can you help me out?
Thanks in advance!!
The JSON that you send is in the request body. The keys/values will not be available in the $_REQUEST array, hence why you are getting the error. You will need to access the request body in order to get the sessionId property.
$requestBody = file_get_contents('php://input');
$session = json_decode($requestBody);
$sesionId = $session["sessionId"];
echo "Session ID: $sessionId";
The file_get_contents('php://input') returns the body of the POST request.
Make sure to also add the appropriate Content-Type header to your post request:
curl -X POST -k -i -H "Accept: application/json" -H "Content-Type: application/json" -w "%{body}" -w "%{http_code}" -d "#/var/lib/jenkins/workspace/myFolder/session.json" http://mypage.com/myFolder/newfile.php`
So I've been tearing my hair out all day regarding this one. I have a curl command which I have working on a windows machine which pulls logs from Cloudflare, this works.
curl -sv -o logname.log.gz -X GET -H "Accept-encoding: gzip" -H "X-Auth-Email: myemail#email.com" -H "X-Auth-Key: 12345" "https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709"
I'm trying to import this into powershell with the end goal of making the start and end time parameters different based on the current time, however I simply cannot get the command to run in powershell, I've tried various different tracks with this being the latest and most simple.
cmd.exe /c 'curl -sv -o logname.log.gz -X GET -H "Accept-encoding: gzip" -H "X-Auth-Email: myemail#email.com" -H "X-Auth-Key: 12345" "https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709"'
Which gives me this error
{ [11971 bytes data]
* Failed writing body (0 != 11963)
* Failed writing data
* Curl_http_done: called premature == 1
* Closing connection 0
* schannel: shutting down SSL/TLS connection with api.cloudflare.com port 443
* schannel: clear security context handle
I already know that my unix timestamp is a bit off and plan on fixing that next but what I cannot understand is why the same command works through the command prompt and not through Powershell.
Would someone be able to help?
Thank you
Dealing with parameters to native commands in PowerShell can be a minefield because you have to deal with quoting and special characters for both, sometimes nested.
It's probably safer to use Start-Process in PowerShell and give it an array of parameters:
Start-Process curl.exe -ArgumentList '-sv','-o','logname.log.gz','-X','GET','-H','Accept-encoding: gzip','-H','X-Auth-Email:','myemail#email.com','-H','X-Auth-Key:','12345','https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709'
But what you should realy do, is check out Invoke-WebRequest which will ultimately be much easier.
$body = #{
start = 1481509909
end = 1481538709
}
$headers = #{
'Accept-Encoding' = 'gzip'
'X-Auth-Email' = 'myemail#email.com'
'X-Auth-Key' = '12345'
}
$response = Invoke-WebRequest -Uri 'https://api.cloudflare.com/client/v4/zones/987654/logs/requests' -OutFile logname.log.gz -Body $body -Headers $headers
Note this is not tested at all, but should be a good starting point.
what is curl -u equivalent in php unirest
Blockscore API:
$ curl https://api.blockscore.com/people \
-u sk_test_n5049aa6053c9a0217bea78070fbf501: \
--header "Accept: application/vnd.blockscore+json;version=4"
Api key: sk_test_n5049aa6053c9a0217bea78070fbf501
I want to try this out in Unirest. but what is -u?
is it a header or a post?
The -u flag for cURL is used to set the credentials for HTTP Basic authentication. The provided credentials will be base64-encoded and presented in the "Authorization" header as in:
Authorization: Basic c2tfdGVzdF9uNTA0OWFhNjA1M2M5YTAyMTdiZWE3ODA3MGZiZjUwMTo=
So it is a header.
In unirest you should be able to use:
Unirest\Request::auth('sk_test_n5049aa6053c9a0217bea78070fbf501', '');
I'm trying to validate a payment made through a partner service. The partner is passing me the payment id and I'm using the REST API to look it up. So far, all attempts to use the payments/payment/<Payment-Id> are throwing an INTERNAL_SERVICE_ERROR. (I'm still using the sandbox and demo user.)
Getting a token seems to be working fine:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token -H "Accept: application/json" -H "Accept-Language: en_US" -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" -d "grant_type=client_credentials"
Response:
{"scope":"https://api.paypal.com/v1/developer/.* https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.*","access_token":"A015GzttpnOSOPfcqtfAaq1kdgqow5W.IXSe6TJ9Cpbesr4","token_type":"Bearer","app_id":"APP-8KK24973T6066201W","expires_in":28800}
Taking that token and putting into the payment request:
curl -v -X GET https://api.sandbox.paypal.com/v1/payments/payment/PAY-8UD377151U702286RKOQ3DTQ -H "Content-Type:application/json" -H "Authorization: Bearer A015GzttpnOSOPfcqtfAaq1kdgqow5W.IXSe6TJ9Cpbesr4"
returns the error:
{"name":"INTERNAL_SERVICE_ERROR","message":"An internal service error has occurred","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INTERNAL_SERVICE_ERROR","debug_id":"ca33c7679121d"}
UPDATE: basically I'm trying to follow the steps outlined here: https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/