using curl in php with bearer authorization - php

im want to use curl with php to send push-messages to an ionic-app. this is my command im using on the commandline:
curl -X POST -H "Authorization: Bearer XXXXXX.AUTH.XXXXXXX" -H "Content-Type: application/json" -d '{
"tokens": ["XXXX.DEVICE_TOKEN.XXXX"],
"profile": "XXXX.PROFILE.XXXXX",
"notification": {
"message": "This is a test-message",
"title": "This is a Test Title"
}
}' "https://api.ionic.io/push/notifications"
how can i execute this command in php? i found some examples, but none of them works.

Related

How to send tweets with curl?

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

JSON POST Empty Using cURL Command

I have a cURL call in a PHP 7.2 script running on CentOS 7 that is passing empty data to an endpoint. In diagnosing the issue, I have tried replicating the issue using cURL on the command-line, and not using the PHP code. Here's the command I am running (I was told to send a Content-Length of 0):
/usr/bin/curl -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Content-Length: 0" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' "https://url.here/endpoint_debug.php"
In the endpoint script I am printing the input stream.
$data = file_get_contents('php://input');
print "HEADERS:\n\n";
print_r(apache_request_headers()) . "\n\n";
print "RAW Data:\n\n";
print "'" . $data . "'\n\n";
No matter what I have done, the $data variable is always empty. The result I see is always ''. I also tried sending it to https://webhook.site/ and it, too, shows that the "Form values" are empty.
I have tried with -X POST and without. I tried changing the order of the calls so the URL is before the data (-d) switch. I have tried --data as well.
What could be the issue? I have replicated this on 2 different servers calling the same endpoint. Other code in the same project handles file_get_contents('php://input') properly, so I don't think that's it.
Any guidance would be appreciated!
TLDR: You need to remove -H "Content-Length: 0"
It doesn't make sense why you were instructed to send this if you want a response.
I tried your command unmodified with a webhook.site url, and it can return the content body (for example) without sending the head, and return nothing if sending the header:
Quite simply, this command:
/usr/bin/curl -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' 'https://webhook.site/27f6128e-2f82-4157-99da-99464f22122f'
Returns the body. Whereas this command:
/usr/bin/curl -H "Content-Length: 0" -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' 'https://webhook.site/27f6128e-2f82-4157-99da-99464f22122f'
Obviously returns nothing
The server response headers indicate as much :
> Accept: application/json
> Content-Type: application/json
> Content-Length: 0
>
* upload completely sent off: 112 out of 112 bytes
< HTTP/1.1 200 OK
< Server: nginx/1.14.2

cURL command to POST with data embedded in request

I am writing a simple shell script using which I will create a request in JIRA when conditions are met using curl. Following is the JIRA way of executing it, which needs me to send send the request as a command and parameters using a data file:
Request:
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
Data:
{
"fields": {
"project":
{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}}}
So I don't want to use a separate data file as shown above. Rather I would like to using a single curl Request which has the data embedded in the request. Something like:
curl -D- -u fred:fred -X POST --"**PASS ALL MY DATA HERE**" -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
I know it will make things cluttered, but thats the way i want it. Can you please suggest if the above is feasible.
Can you do it with a here document?
curl -D- -u fred:fred -X POST -d - -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/ <<EOF
{
"fields": {
"project":
{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}}}
EOF

How to create the get method for the call

I am studying and implementing an api and while following the examples,
Here is the example 1 :
curl -X POST \
-H "Content-Type: application/json" \
-H "QuickBlox-REST-API-Version: 0.1.0" \
-d '{"application_id": "2", "auth_key": "DtF9cZPqTF8Wy9Q", "timestamp": "1333630580", "nonce": "1340569516", "signature": "13293a5bd2026b957ebbb36c89d9649aae9e5503", "user": {"login": "injoit", "password": "injoit"}}' \
https://api.quickblox.com/session.json
For the above example i consructed my get method as
https://api.quickblox.com/session.json?token=re8d22c6e617133ffeadd761193a6c57d87bfb1a0f&application_id=23995&auth_key=CbRasu4Wftu25Qw&nonce=8796&timestamp=1434446627&signature=667ee2b448a5d3dd57d112afef3f84dd6c67e165
and it is working good.
But for the below example
curl -X POST \
-H "QuickBlox-REST-API-Version: 0.1.0" \
-H "QB-Token: 17f6a337b0656c9c7e983f9705d79562fc694c0e" \
-H "Content-Type: application/json" \
-d '{"push_token": {"environment": "production", "client_identification_sequence": "aa557232bc237245ba67686484efab"},
"device": {" platform": "iOS", "udid": "5f5930e927660e6e7d8ff0548b3c404a4d16c04f"}}' \
http://api.quickblox.com/push_tokens.json
How can i create the get method. As this has two main arrays i am little confused.
Can anyone help in creating how can i use this call ?
It should be something like this:
https://api.quickblox.com/push_tokens.json?push_token[environment]=production&push_token[client_identification_sequence]=aa557232bc237245ba67686484efab&device[platform]=iOS&device[udid]=5f5930e927660e6e7d8ff0548b3c404a4d16c04f

How do you upload files in Parse using CURL?

I have the following:
curl -X POST \
-H "X-Parse-Application-Id: appid"
-H "X-Parse-REST-API-Key: restkey"
-H "Content-Type: text/plain"
-d 'Hello, World!'
https://api.parse.com/1/files/hello.txt
And I get back:
{"url":"http://files.parse.com/7680c0e7-b398-4b43-91f0-61b12934c690/08d828ef-1185-4036-b8ab-228764fbeb69-hello.txt","name":"08d828ef-1185-4036-b8ab-228764fbeb69-hello.txt"}
What's the file name I should use?
the URL returned - you take everything after http://files.parse.com/
That comes to the following:
curl -X POST \
-H "X-Parse-Application-Id: appid" \
-H "X-Parse-REST-API-Key: reskey" \
-H "Content-Type: application/json" \
-d '{
"name": "blisdco",
"picture": {
"name": "7680c0e7-b398-4b43-91f0-61b12934c690/08d828ef-1185-4036-b8ab-228764fbeb69-hello.txt",
"__type": "File"
}
}' \
https://api.parse.com/1/classes/CustomerImages
And what is returned is:
{"createdAt":"2012-06-12T20:16:05.360Z","objectId":"yHLhqMgBAj"}
If you look at CustomerImages table in the Parse Data Browser, you'll see the entry - including the file name portion of the URL.

Categories