-3

I want to cocatenate variable in curl code,How can i do this ? I tried with following code but not working for me,Here is my code

$id = $_POST['id'];
$type = $_POST['type'];    

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://xxxxxxxxxxx.com",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_POSTFIELDS => "{\"id\":$id,\"type\":\.$type}}",
);
t1gor
  • 1,244
  • 12
  • 25
amit
  • 1
  • 1
  • 18
  • 28
  • you have a syntax error. – treyBake Apr 26 '19 at 12:51
  • That would be ["interpolation"](https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing), not concatenation. Though [`json_encode`ing](http://php.net/json_encode) the real structure would be better. – mario Apr 26 '19 at 12:52

2 Answers2

2

If you're trying to send JSON, you're better off with encoding an array with json_encode:

CURLOPT_POSTFIELDS => json_encode(["id" => $id, "type" => $type])

If you check your "{\"id\":$id,\"type\":\.$type}}" in a linter, you will see that this json is invalid. The correct json would be "{\"id\":$id,\"type\":\"$type\"}", but as I already said, use json_encode instead.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • ,How can i encode following line ? CURLOPT_POSTFIELDS => "{\"amount\":2,\"currency\":\"KWD\",\"employerid\":\"123\",\"threeDSecure\":true,\"save_card\":false,\"description\":\"Test Description\",\"statement_descriptor\":\"Sample\",\"metadata\":{\"employerid\":$id,\"type\":\"test 2\"},\"reference\":{\"transaction\":\"txn_0001\",\"order\":\"ord_0001\"},\"receipt\":{\"email\":true,\"sms\":true},\"customer\":{\"first_name\":\"test\",\"middle_name\":\"test\",\"last_name\":\"test\",\"full_name\":\"test\",\"email\":\"a@codnostic.com\",\"phone\":{\"country_code\":\"965\"}}" – amit Apr 26 '19 at 13:03
  • use `json_encode`, Amit. You seem like you do not analyse the answers, but only copy-paste them blindly :-/ Learn from what has already been posted. – Kevin Kopf Apr 26 '19 at 13:06
2

You can json_encode these fields,

$temp = json_encode(['id' => $id, 'type' => $type]);

// and pass it to postfields as json encoded string which you are trying to build

    CURLOPT_POSTFIELDS => $temp
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • the keys will be `0` and `1` in that case, which negates with what OP actually wants... – Kevin Kopf Apr 26 '19 at 12:53
  • Thanks mate, I made changes. – Rahul Apr 26 '19 at 12:54
  • Good. And now your answer precisely copies mine :D – Kevin Kopf Apr 26 '19 at 12:54
  • Yeah alias kind, different way. – Rahul Apr 26 '19 at 12:55
  • @RahulMeshram How can i encode following line ? CURLOPT_POSTFIELDS => "{\"amount\":2,\"currency\":\"KWD\",\"employerid\":\"123\",\"threeDSecure\":true,\"save_card\":false,\"description\":\"Test Description\",\"statement_descriptor\":\"Sample\",\"metadata\":{\"employerid\":$id,\"type\":\"test 2\"},\"reference\":{\"transaction\":\"txn_0001\",\"order\":\"ord_0001\"},\"receipt\":{\"email\":true,\"sms\":true},\"customer\":{\"first_name\":\"test\",\"middle_name\":\"test\",\"last_name\":\"test\",\"full_name\":\"test\",\"email\":\"a@codnostic.com\",\"phone\":{\"country_code\":\"965\"}}" – amit Apr 26 '19 at 13:04