美好的一天,我想使用cURL从URL获取响应,然后将数据分配给PHP变量,但即使URL有数据,我的cURL也会返回NULL值。我搜索并尝试了类似情况下的每个解决方案,但我失败了。希望有人可以帮我解决这个问题。
这是网址" http://localhost/sample/testURL.php"
的数据{
"login": {
"error": false,
"user": {
"br_code": 0,
"mem_id": 202
}
},
"branch": {
"error": false,
"branch_info": {
"br_id": 0,
"br_desc": "OFFICE 1",
"br_add": "Sample Address",
"br_manager": "John Doe",
"br_accronym": "",
"br_trans_date": "2017-08-05"
}
},
"accounts": {
"error": false,
"sl_summ": [
{
"sl_desc": "Sampe Account",
"tr_date": "2017-09-04",
"actual_balance": "100.00",
"available_balance": "100.0"
}
]
}
}
这是我的cURL的代码,返回NULL
<?php
session_start();
$url= 'http://localhost/sample/testURL.php';
$ch = curl_init($url);
$br_code= $_SESSION["BR_CODE"];
$client_id= $_SESSION["MEM_ID"];
// set post fields
$post = [
'br_code' => $br_code,
'client_id' => $client_id
];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
print_r(curl_getinfo($ch));
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
// close the connection, release resources used
curl_close($ch);
$obj= json_decode($response, true);
// do anything you want with your response
var_dump($obj); //returns NUll
var_dump($br_code); //returns 0
var_dump($client_id); //returns 202
?>
使用curl_getinfo()
返回值Array
(
[url] => http://localhost/coredev_localserver/accountsummary.php
[content_type] => text/html; charset=UTF-8
[http_code] => 200
[header_size] => 393
[request_size] => 216
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.078
[namelookup_time] => 0.015
[connect_time] => 0.031
[pretransfer_time] => 0.031
[size_upload] => 244
[size_download] => 304
[speed_download] => 3897
[speed_upload] => 3128
[download_content_length] => 304
[upload_content_length] => 244
[starttransfer_time] => 0.031
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => ::1
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => ::1
[local_port] => 52160
)
NULL
int(0)
int(202)