返回curl响应作为对象而不是字符串

时间:2019-02-15 15:37:58

标签: php curl

我正在请求卷曲。它返回字符串而不是json。

案例1)

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));

curl_setopt($ch, CURLOPT_URL,$this->accessTokenUri);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->accessTokenhttpQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

\Log::info($result);
\Log::info(gettype($result));

此打印

  

invalid_request

情况2)没有httpheaders

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$this->accessTokenUri);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->accessTokenhttpQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

\Log::info($result);
\Log::info(gettype($result));

此打印

  

结果和gettype是字符串。

我尝试了

a)json_encode($result)表示无法将std类的对象转换为字符串。

b)json_decode(json_encode($result, true));-仍然无法工作

最后一名

$tokenGrantParams =  array(
    "grant_type" => "test1",
    "client_id" => "test2",
    "client_secret" => "test3"
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"urlhere");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($tokenGrantParams));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

$d = json_decode($result);
return $d;

我该如何获取请求作为对象?

0 个答案:

没有答案