send json request using php for and api - php

I am working on some api in which I send information using json.
POST /api/v3/users/ HTTP/1.1
Host: example.com
Content-Type: application/vnd.api+json
Authorization: Bearer ab1dcb45be7e43db2212b0d2b0ca3892
This is the information. Using this will get that response but I don't know how to use this in php and not use js?
get response:
{
"users": [ {
"username": "username",
"phone": "06123",
"last_name": "myname",
"address": "some addr",
"newsletter": true,
"dob": "1974-12-31",
"x_id": "123ext",
"email": "user2#mail.nl",
"email_verified": true,
"social" : {
"facebook": { "id": "100000730165158"},
"twitter": {"id": "139029213"},
"linkedin": {"id": "7EA06I71K5"},
"google": {"id": "104763295979778440139"}
}
}]
}
But I don't know how to use this, meaning how to send request.

You can store this content in avariable, and then just json_decode it to convert it into php array.

Related

PHP able to receive post request with a raw text but its giving error when post same data raw json

I'm posting data to my API endpoint. it's simple JSON data. But I'm getting error 404 when posting data as raw JSON but if I post the same data at the same endpoint as raw text its works.
working as raw text
getting error 404 as raw JSON
<?php
var_dump(http_response_code());
echo "hello";
var_dump($_POST);
echo file_get_contents("php://input");
I have removed all code from the API end point and just trying to print post data.
Sample JSON :
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "456",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "123456789",
"phone_number_id": 123456789
},
"contacts": [
{
"profile": {
"name": "NAME"
},
"wa_id": 123456789
}
],
"messages": [
{
"from": 123456789,
"id": "wamid.ID",
"timestamp": 123456789,
"text": {
"body": "MESSAGE_BODY"
},
"type": "text"
}
]
},
"field": "messages"
}
]
}
]
}
I have checked JSON and it's a valid JSON. no special character but still unable to solve this issue.
One more thing its working on my local machine under XAMPP but not on Linux shared server.

Send Current URL link to A Discord Webhook

So, basically I need a way to send the current URL link to A Discord webhook.
So for example, the URL would be https://www.example.com and I need that link to send to my Discord webhook.
Is there any way someone could help me with this? If this is not possible, are there any alternative methods?
I would recommend you first reading this—it explains in detail how to format the payload so that it renders as a message in Discord properly, you can even use markdown!
Second, I'm not sure what language you want to code this in, but here's a simple example using Python and httpx
The idea to take away here is
Pick a client library
Study the Discord webhook message format
Create a client for HTTP
Set the logic to send the payload to the webhook you want
Create and send the payload with the formatted webhook message in a POST request
data = {
"username": "Webhook",
"avatar_url": "https://i.imgur.com/4M34hi2.png",
"content": "Text message. Up to 2000 characters.",
"embeds": [
{
"author": {
"name": "Birdie♫",
"url": "https://www.reddit.com/r/cats/",
"icon_url": "https://i.imgur.com/R66g1Pe.jpg"
},
"title": "Title",
"url": "https://google.com/",
"description": "Text message. You can use Markdown here. *Italic* **bold** __underline__ ~~strikeout~~ [hyperlink](https://google.com) `code`",
"color": 15258703,
"fields": [
{
"name": "Text",
"value": "More text",
"inline": true
},
{
"name": "Even more text",
"value": "Yup",
"inline": true
},
{
"name": "Use `\"inline\": true` parameter, if you want to display fields in the same line.",
"value": "okay..."
},
{
"name": "Thanks!",
"value": "You're welcome :wink:"
}
],
"thumbnail": {
"url": "https://upload.wikimedia.org/wikipedia/commons/3/38/4-Nature-Wallpapers-2014-1_ukaavUI.jpg"
},
"image": {
"url": "https://upload.wikimedia.org/wikipedia/commons/5/5a/A_picture_from_China_every_day_108.jpg"
},
"footer": {
"text": "Woah! So cool! :smirk:",
"icon_url": "https://i.imgur.com/fKL31aD.jpg"
}
}
]
}
import httpx # pip install httpx, or poetry add httpx
with httpx.Client() as client:
request = client.post("https://www.discordwebhook.com", data=data) # needs to be a POST
print(request.status_code)

API gives 406 code with Accept application/json request : Bluehost

This is the result I get when I try to validate my API.
Content-Type: application/json
Vary: Accept
{
"valid": false,
"token_uri": "https://mining4btc.com/NFT/rinkeby/CKE/1",
"errors": [
"InvalidTokenUrlResponseException: Invalid response, expected 200 but got 406 for URL: https://mining4btc.com/NFT/rinkeby/CKE/1"
]
}
I'm using this to validate my API:
https://testnets-api.opensea.io/asset/0xe8dD349E3B0F0FA0eE063a2D99541155aFEf14B9/1/validate/
As you can see, it's getting a 406 code.
This is the API:
<?php
header('Content-Type: application/json');
$output = '{
"attributes": [
{
"trait_type": "Eyes",
"value": "cute"
},
{
"trait_type": "Nose",
"value": "cute"
},
{
"trait_type": "Cheeks",
"value": "cute"
},
{
"trait_type": "Toes",
"value": "cute"
},
{
"trait_type": "Ability",
"value": "chairs"
},
{
"display_type": "boost_number",
"trait_type": "Cuteness",
"value": 5000
},
{
"display_type": "number",
"trait_type": "Age",
"value": 1
}
],
"description": "Thinking about my Mommy.",
"external_url": "https://mining4btc.com/NFT/rinkeby/CKE/external",
"image": "https://mining4btc.com/NFT/rinkeby/CKE/proof/1.jpg",
"name": "Chillin"
}';
$decoded = json_decode($output);
echo json_encode($decoded);
?>
This is a contract API for a NFT following the OpenSea documents; deployed with Truffle to rinkeby.
I have confirmed that this is because Bluehost actively blocks (by returning this code) requests with Accept:application/json headers. You have to contact Bluehost and ask them to whitelist the IP addresses that you want to be able to access the metadata.
As far as I can tell, they do this because they want to push people to pay for a VPS, because they are assuming this kind of request would go to some kind of robust API that requires significant backend processing.

API json postman and drupal 8

How are you my friends ?
i have an issue with API with drupal 8 in post method
when i send the request the respond is 406 Not Acceptable
this is the code
POST /movies/entity/node?_format=hal_json HTTP/1.1
Host: abdallah.tech
X-CSRF-Token: t1UOL3VNN0GRGSNxWN************************
Authorization: Basic ************************
Cache-Control: no-cache
Postman-Token: 4f61c400-4916-4dfb-8ca7-0ed08c48a4bc
{
"_links": {
"type": {
"href": "http://abdallah.tech:8083/movies/rest/node/add/movies"
}
},
"type": [{
"target_id": "movies"
}],
"title": [{
"value": "hello world"
}],
"body": [{
"value": "some body content aaa bbb ccc"
}]
}'
can you help me with that please
just try to change
POST /movies/entity/node?_format=hal_json HTTP/1.1
to
POST /movies/node?_format=hal_json
// or
POST /node?_format=hal_json

Gmail API return chats with "SENT" label

Using the Gmail API call
https://www.googleapis.com/gmail/v1/users/userId/messages/id
return chats with label SENT
I have used the following code for getting the message content
$url = "https://www.googleapis.com/gmail/v1/users/$useremail/messages/$messageid?format=full";
$response = $gmailInstance->performHttpRequest($gmailAuthObj, $useremail, $url);
$responseBody = json_decode($response->getResponseBody(), TRUE);
I got the following response for the above API call
{
"id": msgid,
"threadId": threadid,
"labelIds": [
"CHAT",
"SENT"
],
"snippet": "sometext",
"historyId": 7 digit numeric,
"internalDate": "1371160444361",
"payload": {
"partId": "",
"mimeType": "text/html",
"filename": "",
"headers": [{
"name": "From",
"value": "Test User \u003ctuser#tdom.com\u003e"
}],
"body": {
"size": 20,
"data": somerandomstring
}
},
"sizeEstimate": 100
}
Does anybody know why the API response return chat message with SENT label in it? Is there a way to avoid this (SENT label from chat response)?

Categories