M
M
muhasa2020-01-28 16:05:23
PHP
muhasa, 2020-01-28 16:05:23

Yandex.Cloud cannot send a request via php curl, everything works in the console, can you help?

curl  -H "X-Client-Request-ID: df55b979-346c-49a1-92c2-ac42730zzzz4" \
    -H "Authorization: Bearer token=" \
    -d "{\"folder_id\": \"blablablac\", \"texts\": \"классика\", \"sourceLanguageCode\": \"ru\", \"targetLanguageCode\": \"en\"}" \
    -X POST "https://translate.api.cloud.yandex.net/translate/v2/translate"

Here is the code through the console gives a normal result
. If I do the same through php curl, it returns some kind of garbage
Unexpected token.
sourceLanguageCode=r
^


The php code is as follows:

$data = http_build_query([   
        "sourceLanguageCode" => "ru",
        "targetLanguageCode" => "en", 
        "texts" => "привет",
        "folder_id" => $folder_id
    ]);

    $headers = [
        "Content-Type: application/json",
        "Authorization: Bearer ".$token
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);

    $server_output = curl_exec ($ch);

    $info = curl_getinfo($ch);
    print_r($info['request_header']);

    var_dump($server_output);

    curl_close ($ch);


I can't figure out what's the difference?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question