passing json data as post

i am new to api so i may be completely wrong. i was going through some docuementaion in github but could not find some answers so i am here

i want to pass url of these functions to api.php after validating these key and secret.when i echo these data i get key, secret and url but how to get these details in php as its not a post and i cant use _post function to manipulate data based on url submitted and give the result

   public function __construct($key = '', $secret = '', $timeout = 30, 
  $proxyParams = array()) {
    $this->auth = array(
        "auth" => array(
            "api_key" => $key,
            "api_secret" => $secret
        )
    );
    $this->timeout = $timeout;
    $this->proxyParams = $proxyParams;
}

public function url($opts = array()) {
    $data = json_encode(array_merge($this->auth, $opts));

//  echo $data;
    $response = self::request($data, 'http://somesite.com/a/api.php', 'url');

    return $response;
}

here is request function

 private function request($data, $url, $type) {
        $curl = curl_init();

        if ($type === 'url') {
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json'
            ));
        }

        curl_setopt($curl, CURLOPT_URL, $url);

        // Force continue-100 from server
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36");
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_FAILONERROR, 0);
        curl_setopt($curl, CURLOPT_CAINFO, __DIR__ . "/cacert.pem");
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);

        if (isset($this->proxyParams['proxy'])) {
            curl_setopt($curl, CURLOPT_PROXY, $this->proxyParams['proxy']);
        }

        $response = json_decode(curl_exec($curl), true);

        if ($response === null) {
            $response = array (
                "success" => false,
                "error" => 'cURL Error: ' . curl_error($curl)
            );
        }

        curl_close($curl);

        return $response;
    }
}

output of echo data is sufficient but its not post and i tried json_decode but nothing is coming to api.php

here is output of echo

        {"auth":{"api_key":"be8fgdffgrfffrffc4b3","api_secret":"1b59fsfvfrgfrfvfb29d6e555a1b"},"url":"https:\/\/i.ndtvimg.com\/i\/2017-06\/modi-at-kochi-metro-station_650x400_81497685848.jpg","wait":true}

i tried these in api.php to get the data but nothing is working

$gggss['url'] = json_decode($data, true);   //this returns an array

or

 $gggss=$_POST['data'];

any help will be great

1 answer

  • answered 2017-06-17 19:17 Nurik Nurmetov

    I think you are trying get urlencoded data, while your JSON string located in body of request. Try use this instead: $entityBody = file_get_contents('php://input');