I would like to be able to turn a PHP function into a variable that I can use inside of a JSON string.
My PHP function is so:
<?php
function output(){
echo "<pre>";
foreach($_POST as $key => $value){
if(strpos($key,'HIDEME') != true){
echo "<span style='color: black;'><strong>",$key,"</strong>
</span> <span style='color: #2555A4;'>",$value,"</span><br/>";
}
}
echo "</pre>";
}
$output = output();
echo json_encode($output);
?>
The function(output(); is what I wish to turn into $output. The purpose of this function is to echo key and value of a submitted form. I have a attempted to turn the output(); into $output, and then use json_encode to hopefully use it inside of the following...
My PHP cURL for JSON is so:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "BLAHBLAHBLAH.COM",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n\"TicketId\": 141,\r\n\"Note\":".json_encode($output).",\r\n\}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$access_token,
"Cache-Control: no-cache",
"Content-Type: application/json",
"Postman-Token: 577b3128-0836-26e8-c6ff-9619f952f820"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
The purpose of the cURL is to post a support ticket note using the function output();.
My problem is that while the form keys/values do echo with the function alone, turning the function into $output reports "NULL".
What is the proper way to turn a PHP function into a variable that I can then use for posting a ticket note in cURL/JSON?
Inside the output()
function you print the string instead of returning it, maybe this helps you
function output() {
$out = "<pre>";
foreach($_POST as $key => $value) {
if(strpos($key, 'HIDEME') != true) {
$out .= "<span style='color: black;'><strong>".$key."</strong>"
. "</span> <span style='color: #2555A4;'>".$value."</span><br/>";
}
}
$out .= "</pre>";
return $out;
}
With .=
you can append stuff to a string
$a = 'foo'; // 'foo'
$a .= 'bar'; // 'foobar'
Also: to say "turn a php-function into a json-variable" is kind of misleading here. Actually you encode the return value of a php-function to json.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.