试图操纵从 cURL 调用返回的 json
Trying to manipulate the returned json from a cURL call
这个问题让我很困惑。这是我的代码:
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);
}
我正在使用浏览器 运行 php 脚本,输出似乎是 json 格式,这正是我需要的。然而,当我添加 json 解码并尝试 print_r 或 var_dump 输出时,我只是得到一个 boolean(true) 或 int(1),它不想将数据放入大批。同样奇怪且最有可能相关的是,上面的代码没有使用 print_r,或 var_dump,甚至没有回显,但它仍然将 json 格式打印到屏幕上?
如有任何帮助,我们将不胜感激。
编辑
这是出现在屏幕上的 json 输出:
{"AvailableAdvisers":[{"AdviserId":"1345678","BusinessEntityName":"Bob Smith Finance","FullName":"Bob Smith"},{"AdviserId":"12345678","BusinessEntityName":"Globe Home Loans Pty Ltd","FullName":"Jane Doe"}],"FirstName":"Sam","LastName":"Sung","LendingPartnerStaffId":"12345674356","Locations":[{"LendingPartnerLocationId":"123467867647","Name":"Bank"},{"LendingPartnerLocationId":"12324354545","Name":"Jane Smith"}],"UserName":"Username"}
好的,看来我明白你的意思了。
因此,要将 curl 响应放入 $content
变量中,您可能需要添加行:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
在你的curl_exec
之前
但是 curl 响应如何出现在页面上的奇迹是真正的谜:-)
这个问题让我很困惑。这是我的代码:
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);
}
我正在使用浏览器 运行 php 脚本,输出似乎是 json 格式,这正是我需要的。然而,当我添加 json 解码并尝试 print_r 或 var_dump 输出时,我只是得到一个 boolean(true) 或 int(1),它不想将数据放入大批。同样奇怪且最有可能相关的是,上面的代码没有使用 print_r,或 var_dump,甚至没有回显,但它仍然将 json 格式打印到屏幕上?
如有任何帮助,我们将不胜感激。
编辑
这是出现在屏幕上的 json 输出:
{"AvailableAdvisers":[{"AdviserId":"1345678","BusinessEntityName":"Bob Smith Finance","FullName":"Bob Smith"},{"AdviserId":"12345678","BusinessEntityName":"Globe Home Loans Pty Ltd","FullName":"Jane Doe"}],"FirstName":"Sam","LastName":"Sung","LendingPartnerStaffId":"12345674356","Locations":[{"LendingPartnerLocationId":"123467867647","Name":"Bank"},{"LendingPartnerLocationId":"12324354545","Name":"Jane Smith"}],"UserName":"Username"}
好的,看来我明白你的意思了。
因此,要将 curl 响应放入 $content
变量中,您可能需要添加行:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
在你的curl_exec
但是 curl 响应如何出现在页面上的奇迹是真正的谜:-)