Postman post doesn't send any data - php

I'm testing an api using Postman but it doesnt send any data.
When I create a test using php function it works fine.
the api php
$json_str = file_get_contents("php://input");
$obj = json_decode($json_str, false);
the test php
$obj->CompanyCode= "39170";
$obj->CustomerNumber= "1800001";
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $obj ),
'header'=>array(
"Content-Type: application/json",
"Accept: application/json")
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "https://www.url.com/inquiry/", false, $context );
the postman
postman header
postman body

Related

1msg error when using sendMessage endpoint api

Hello I'm trying to use the 1msg API for my company to send WhatsApp messages, but I get the following error:
I'm using the correct endpoint any of you have idea to correct this bug
<?php
// Define the endpoint URL
$url = "https://api.1msg.io/$channelId/sendMessage?token=$tokenId";
// Define the message data
$data = array(
"phone" => "$num",
'body' => array(
"text" => "this is a test message.",
)
);
// Use http_build_query to format the data as url encoded string
$postdata = http_build_query($data);
// Prepare the options for the request
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $postdata
)
);
// Create a stream context for the request
$context = stream_context_create($options);
// Send the request
$response = file_get_contents($url, false, $context);
> Print the response
print_r($response)
?>

PHP: Using file_get_contents to access an API - Empty Response

Sorry for the messy code in advance. I want to write a code which returns me infos from the official Blizzard API, which I can then print out on my homepage. The code doesn't throw any errors but it doesn't print out something either. For Starters:
I would also prefer using CURL, but my homepage is on a Wordpress Hosting Site and I don't know how to install the CURL Library that way
allow_furl_open is on
$url = "https://eu.battle.net/oauth/token";
$data = array('grant_type' => 'client_credentials');
//HTTP options
$opts = array('http' =>
array(
'method' => 'POST',
'header' => array ('Content-type: multipart/form-data', 'Authorization: Basic ' .
base64_encode("$client_id:$client_pass")),
'content' => json_encode($data)
)
);
//Do request
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
$accessToken = $json['access_token'];
$tokenURL = "https://us.api.blizzard.com/data/wow/token/?namespace=dynamic-eu";
$opts2 = array('http' =>
array(
'method' => 'POST',
'header' => array('Content-type: multipart/form-data', 'Authorization: Bearer ' . $accessToken),
)
);
$context2 = stream_context_create($opts2);
$json2 = file_get_contents($tokenURL,false,$context2);
$result2 = json_decode($json2, true);
$tokenprice = $result2['price'];
echo "<p>Tokenpreis:" .$tokenprice. "</p>";
I didn't add the $client_id and $client_pass into the code snippet, but this exists obviously. I used this PHP CURL Snippet as template. And this is a short explanation on blizzard's site on how is this supposed to work:
Anyone got any ideas what went wrong? I am really out of ideas here and would love anyone who could help.
Thanks in advance
Based on the curl example from the linked API docs, the content type should be application/x-www-form-urlencoded, and so for the initial request to https://eu.battle.net/oauth/token you should be able to follow the example here.
In your case this will look something like:
$url = 'https://eu.battle.net/oauth/token';
$data = array('grant_type' => 'client_credentials');
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array (
'Content-type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode("$client_id:$client_pass")
),
'content' => http_build_query($data)
)
);
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
Also, in your code you are storing the raw result from the initial request in $json, then the decoded array in $result, but attempting to get the access_token from the initial request, instead of the decoded array.

How to make POST Form call in php + json

I want to make a POST call from PHP to my python API
here is the equivalent in Python:
import requests
import json
payload = json.dumps({'serviceID':'131001086184'})
r = requests.post('http://17.../todo/api/line/v1.0/tasks', data=dict(data=payload))
print r.content
Works like a charm.
Here the equivalent in curl:
curl -i -F data='{"serviceID":"131001086184"}' http://17.../todo/api/line/v1.0/tasks
Works too.
Now PHP:
<?php
$url = 'http://17.../todo/api/line/v1.0/tasks';
$postData = array('serviceID' => '131001086184');
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n"
'content' => json_encode($postData)
)
));
$response = file_get_contents($url. FALSE. &context)
$responseData = json_decode($response, TRUE);
echo $responseData['published'];
?>
Does not work and I dont get error message.
What am I missing?
In your code $response = file_get_contents($url. FALSE. &context)
You are concatenating $url with FALSE and &context,
Use the following:-
$response = file_get_contents($url, FALSE, &context);

Accessing Content sent via stream header context

Using the following:
$auth = array(
'iAmWhiteListed' => 'me',
'otherStuff' => 'mystuff'
);
$sendAuth = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $auth ),
'header' => "Content-Type: application/json\r\n" . "Accept: application/json\r\n"
)
);
$authContext = stream_context_create( $sendAuth );
$authResult = file_get_contents( $url, false, $authContext );
How does a PHP script access the data send in the content of the http request? (In this case to verify the data therein and send back an appropriate response?)
When using Content-Type: application/json, the $_POST array is not populated. To fill it, I had to grab the input using php://input and decode it back into array format.
$data = file_get_contents("php://input");
$_POST = json_decode($data, true);

How to send a gcm message in php without using cURL?

I'm using the following code to make HTTP POST request to the gcm server.
The code always returns "Unauthorized Error 401". I read that it is about the headers but can't figure out whats wrong.
Can anyone tell me what's wrong?
Is there any other way to send a gcm message?
Any help would be appreciated.
$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";
$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));
$headers = array('Authorization: key=' . $api_key,'Content-Type: application/json');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Tested your code with my API key. I am able to send request to GCM and getting proper response. It seems like your API Key is having the problem
<?php
$msg_url = "http://msg.com";
$msg_title = "message_title";
$ids = array('reg_id_1', 'reg_id_2');
$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";
$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));
$headers = array('Authorization: key=' . $api_key,'Content-Type: application/json');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>

Categories