1

Добрый день, подскажите как отправить в теле запроса свой JSON, есть стандартный скрипт

<?php
/**
* http://www.php.net/manual/ru/function.curl-exec.php
*/
/**
* Send a POST request using cURL
* @param string $url to request
* @param array|string $post values to send
* @param array $options for cURL
* @internal param array $get
* @return string
*/
function curl_post($url, $post = null, array $options = array()) {
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_SSL_VERIFYHOST =>0,//unsafe, but the fastest solution for the error " SSL certificate problem, verify that the CA cert is OK"
        CURLOPT_SSL_VERIFYPEER=>0, //unsafe, but the fastest solution for the error " SSL certificate problem, verify that the CA cert is OK"
        CURLOPT_POSTFIELDS => $post
    );
    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));

    if( ! $result = curl_exec($ch)){
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}

и мне нужно на этот адрес

curl_post("https://iiko.biz:9900/api/0/orders/add?access_token=$access_token&requestTimeout=10000");

Добавить ещё свой JSON, как это сделать?) Заранее спасибо!

1 ответ 1

3

Вам надо добавить корректный CURLOPT_HTTPHEADER. Можете так:

<?php

$url = 'https://iiko.biz:9900/api/0/orders/add?access_token=$access_token&requestTimeout=10000';  
$ch = curl_init($url);
$jsonDatas = array(
    'key' => 'value',
    'key2' => 'value2'
);

$jsonDatasEncoded = json_encode($jsonDatas);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
$result = curl_exec($ch);

?>
1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.