cornacum Posted July 26, 2023 Share Posted July 26, 2023 I am running a php curl postman API in a procedural php script curl_setopt_array($curl, array( CURLOPT_URL => API_SERVER.'/url/SaveUser', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{"user":"user01","name":"John Doe","card":"0","password":"","privilege":"14","deviceSerial":"xxxxxxxxxxx"}', CURLOPT_HTTPHEADER => array( API_TOKEN , 'Content-Type: text/plain' ), )); The post above works normally, but I need to POST a value into the CURLOPT_POSTFIELDS. I tried using variable name, but the value that is sent is the actual variable name, and not the value that the variable holds. $user_id = $_POST[user_id]; CURLOPT_POSTFIELDS =>'{"user":"$user_id","name":"John Doe","card":"0","password":"","privilege":"14","deviceSerial":"xxxxxxxxxxx"}', Can anyone please advise? Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/317126-passing-post-value-to-curlopt_postfields/ Share on other sites More sharing options...
Solution Barand Posted July 26, 2023 Solution Share Posted July 26, 2023 Perhaps create an array then json_encode it EG $user_id = 1234; $data = [ 'user_id' => $user_id , 'name' => 'John Doe' , 'card' => 0 ]; CURLOPT_POSTFIELDS => json_encode($data), Quote Link to comment https://forums.phpfreaks.com/topic/317126-passing-post-value-to-curlopt_postfields/#findComment-1610748 Share on other sites More sharing options...
cornacum Posted July 27, 2023 Author Share Posted July 27, 2023 That works! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/317126-passing-post-value-to-curlopt_postfields/#findComment-1610764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.