admin管理员组

文章数量:1122846

I have a little API script

I'm trying to post from a form on the same page ACTIVE or SUSPENDED.

But show text in the curl script. but its not showing $variable and just the code I put in

<?php
$variable = $_POST['name'];
echo $variable;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '....');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'accept: application/json',
    'Content-Type: application/json',
    'Authorization: Bearer ...',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "state": "<?php echo $variable; ?>"}');

$response = curl_exec($ch);

print_r($response);

curl_close($ch);

?>

<form method="POST" action="suspend.php">
    <input type="text" name='name' value="some value">
    <button type="submit">Go</button>
</form>

I have a little API script

I'm trying to post from a form on the same page ACTIVE or SUSPENDED.

But show text in the curl script. but its not showing $variable and just the code I put in

<?php
$variable = $_POST['name'];
echo $variable;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '....');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'accept: application/json',
    'Content-Type: application/json',
    'Authorization: Bearer ...',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "state": "<?php echo $variable; ?>"}');

$response = curl_exec($ch);

print_r($response);

curl_close($ch);

?>

<form method="POST" action="suspend.php">
    <input type="text" name='name' value="some value">
    <button type="submit">Go</button>
</form>

Share Improve this question edited Nov 23, 2024 at 16:53 halfer 20.4k19 gold badges108 silver badges200 bronze badges asked Nov 21, 2024 at 20:00 David RowlandDavid Rowland 172 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

<?php echo doesn't work inside a string. It only works after you've exited PHP mode with ?>.

Create an array containing the variable and use json_encode().

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["state" => $variable]));

本文标签: PHP form post POST amp showing in curlStack Overflow