Python Post to PHP Post

I am wondering if this is possible to make in PHP with curl or file_get_contents.
I have some Python code:
body = json.dumps({
'agent': {
'name': 'Minecraft',
'version': 1
},
'username': email,
'password': password,
'clientToken': "fff"
})
r = requests.post(url="https://authserver.mojang.com/authenticate", headers=header, data=body)
What would be the equivelent in CURL? Cause i've tried some variations in PHP but can't get it to work.
PHP Code, that i have tried:
$parameters = [
'agent' => [
'name' => 'Minecraft',
'version' => 1
],
'username' => $email,
'password' => $password,
'clientToken' => 'fff'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://authserver.mojang.com/authenticate");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$parameters); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
But i get response:
{"error":"JsonParseException","errorMessage":"Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: (org.eclipse.jetty.server.HttpInputOverHTTP); line: 1, column: 3]"}
If anyone has any insight on this please reply on this question!
Answer
Solution:
Your problem is in the creation of the authentication object. You're creating a PHP array (you need an object), and you're not converting to JSON before sending.
Create the authentication object as an object, then usejson_encode()
to create a JSON string, and use that as your payload.
<?php
// Display some error s for this development code
error_reporting(E_ALL);
ini_set('display_errors',1);
// Create the authentication object
$params = (object)[];
$params->agent = (object)['name'=>"MineCraft", 'version'=>'1'];
$params->username = "email";
$params->password='password';
$params->clientToken='fff';
// Now create the payload for cURL to use in its POST
$payload = json_encode($params);
// Set headers to match payload
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://authserver.mojang.com/authenticate");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$curlInfo = curl_getinfo($ch);
curl_close ($ch);
// Ok - see what we got.
echo "
This code gives this result:
{-code-3}
Since I don't have a user name or password at Mojang this is what I would expect
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot access offset of type string on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.