传递json数据作为帖子

时间:2017-06-17 19:17:14

标签: php json

我是api的新手,所以我可能完全错了。 我在github上经历了一些文档,但找不到一些答案,所以我在这里

我想将这些函数的url传递给api.php 在验证这些密钥和秘密之后。当我回显这些数据时,我得到密钥,秘密和网址,但如何在php中获取这些细节,因为它不是帖子,我不能使用_post函数根据提交的网址操作数据并给出结果< / p>

   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;
}

这是请求功能

 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;
    }
}

回显数据的输出已经足够了,但它没有发布,我尝试了json_decode,但没有任何内容来到api.php

这里是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}

我在api.php中尝试了这些来获取数据,但没有任何工作

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

 $gggss=$_POST['data'];

任何帮助都会很棒

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试获取urlencoded数据,而您的JSON字符串位于请求正文中。请尝试使用此代码: $entityBody = file_get_contents('php://input');

相关问题