Php 尝试操作从cURL调用返回的json

Php 尝试操作从cURL调用返回的json,php,json,curl,Php,Json,Curl,这个问题把我弄糊涂了。这是我的密码: try { //API Url $url = 'https:<REST OF URL>'; //Initiate cURL. $ch = curl_init($url); //The JSON data. $jsonData = array( 'UserName' => '<HIDDEN>', 'Password' => '<HI

这个问题把我弄糊涂了。这是我的密码:

try {
    //API Url
    $url =   'https:<REST OF URL>';

    //Initiate cURL.
    $ch = curl_init($url);

    //The JSON data.
    $jsonData = array(
        'UserName' => '<HIDDEN>',
        'Password' => '<HIDDEN>'
    );

    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);

    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);

    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    //Execute the request
    $content = curl_exec($ch);

catch(Exception $e) {   
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}

好吧,看来我明白你的意思了

因此,要将curl响应转换为
$content
变量,您可能需要添加以下行:

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
在您的
curl\u exec


但是卷曲反应出现在页面上的奇迹是一个真正的谜:-)

我看不出有什么可以讨论的。所以你有了你的
$content
——你的
var\u dump($content)并且它是
true
值,但您想将true转换为什么???也许你应该让我们看看另一边的代码?生成对您的请求的响应,或者您使用什么服务?阅读文档,您可能需要更改一些请求参数?浏览器显示API返回了一个json字符串,因此可以预期json字符串是变量$content。但情况并非如此,当您打印r($content)时,它只显示整数1。所以我需要json字符串是$content,你明白我的意思吗?不,绝对是第一。你不需要请求2。请求成功执行并返回您放入
$content
变量中的内容。3.您确实打印了这个
$content
,您看到它只是
int(1)
。及。。。。。不知何故,我不知道你的观点基于什么原因,但你肯定这个回答是不完整的。但事实表明它是完整的。:-)没有什么要补充的:-)好的。我编辑了这篇文章,当我运行上面的代码时,这就是输出。这不是json吗?我在代码中的何处设置了回显/打印此数据?这是否表明返回的json字符串是$content的内容?但是,当我打印r($content)时,它的值是1。换句话说,哪个变量包含上面的json字符串?告诉我,这很奇怪,它是如何出现在页面上的。哦,好吧,P:-)祝你的项目好运!我不知道如果没有任何
echo
,它怎么会出现:-(
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);