0

Good day, I want to get the response from a URL using cURL then assign the data to PHP variables but my cURL returns NULL value even the URL has data. I searched and tried every solution in a similar situation but I fail . Hope someone can help me fix this problem.

this is the data of the URL "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"
      }
    ]
  }
}

this is the code of my cURL that returns 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
?>

return value with 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)      
Novice
  • 77
  • 2
  • 11
  • 1
    You should dump out the value of `$response` *before* you `json_decode` it. I'd wager there's some invalid JSON somewhere. – ceejayoz Sep 04 '17 at 02:34
  • Is `$response` also empty? I'd guess the `json_decode` is failing, look for errors, http://php.net/manual/en/function.json-last-error.php. – chris85 Sep 04 '17 at 02:34
  • when I tried to var_dump($response), it returns **string(0) ""** – Novice Sep 04 '17 at 03:10
  • when I var_dump(json_last_error(), json_last_error_msg()); it returns **int(4) string(12) "Syntax error"** – Novice Sep 04 '17 at 03:14
  • Closed as that's the problem – Lawrence Cherone Sep 04 '17 at 04:04
  • why is it marked as duplicate? cURL only returns null and that's the problem. and hes question is: When I use curl via POST and set CURLOPT_POSTFIELD do I have to urlencode or any special format? he did not say that it returns null he only ask if it has any special format. – Novice Sep 04 '17 at 05:14

0 Answers0