M
M
Max2015-09-23 19:01:23
PHP
Max, 2015-09-23 19:01:23

Php, json, urlencode - don't understand?

Hello.
One hosting has its own api - I screwed it up and everything seems to be OK but (!)
When the server sends an error report (for example, because an address with the same name already exists, I get the following message "{"status":"error" ,"message":"\u041f\u043e\u0447\u0442\..." \u041f\ u043e
\u0447\u0442\... is urlencoded UTF-8 encoded text. this API is written:
attachment.php?attachmentid=513&stc=1&d=attachment.php?attachmentid=514&stc=1&d=

The response to a POST request is always returned in UTF-8 encoding. Can be returned in JSON or XML format. The documentation only covers examples in JSON format.
The response always contains a status field, which can be error or success.
error: means that an error occurred while trying to execute the request. In this case, a detailed description of the error will be returned in the message field.
{"status":"error","message":"A mailbox named test already exists."}
success: The request was successful. In this case, depending on the operation performed, the server may return additional data in the data field.
{"status":"success","data":"219493"}

That is, as far as I understand, you first need to [url] http://php.ru/manual/function.json-decode.html[/url] , then take a specific array element with the response and apply [url] http to it ://php.net/manual/en/function.urldecode.php[/url]
My request
<?php
$fields = array(
    'auth_login' => "moi_login",
    'auth_token' => "moi_token",
   
    'class' => "hosting_mailbox",
    'method' => "create",
   
    'account' => "moi_acc", 
    'mailbox' => $_POST['mailbox'],
    'password' => $_POST['password'],
);

$fields_string = ""; 
foreach($fields as $key => $value) {
     $fields_string .= $key.'='.urlencode($value).'&';
}

rtrim($fields_string, '&');
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://HOSTING/api.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$response = curl_exec($ch);
curl_close($ch);
echo "Ответ на Ваш запрос: ".$response;
?>

The problem is that I don't know much about php and js. urldecode paired with json-decode is too complicated for me.
Help me finish :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2015-09-23
@WarStyle

this is urlencoded text in UTF-8 encoding.

Take it higher. This is one whole JSON. JSON encodes non-ASCII characters in the same way. After json_decode, no urldecode is needed anymore.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question