最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to use POST method using custom wordpress button?

programmeradmin1浏览0评论

everyone!

I'm pretty new to wordpress development, and right now i'm trying develop a small ecommerce site using some wordpress plugins and some php on my part.

Right now i'm in the middle of developing a feedback page to pass on to freshdesk using API.

I tried it on localhost, and it's working. Freshdesk receives the ticket and everything, but when I do it in wordpress, it's not passing on the data.

Here's the simple post method using forms

<div class="feedback-form">
    <form action="includes/feedback.inc.php" method="POST">
        <font class="fdeskfont">Full Name</font>
        <br>
        <input type="text" name="fullname">
        <p></p>
        <font class="fdeskfont">Email</font>
        <br>
        <input type="text" name="email">
        <p></p>
        <font class="fdeskfont">Subject</font>
        <br>
        <input type="text" name="subject">
        <p></p>
        <font class="fdeskfont">Message</font>
        <br>
        <textarea type="text" name="description"></textarea>
        <p></p>
        <button type="submit" name="submit">Submit</button>
    </form>
</div>

And here's the include:

$api_key = "API_KEY";
$password = "MY_PASSWORD";
$yourdomain = "";
$custom_fields = array("cf_full_name" => "Sample_Name");
$ticket_data = json_encode(array(
  "description" => "Some details on the issue ...",
  "subject" => "Support needed..",
  "email" => "[email protected]",
  "custom_fields" => $custom_fields,
  "priority" => 1,
  "status" => 2,
));
$url = "https://MY_DOMAIN/api/v2/tickets";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201) {
  echo "Ticket created successfully, the response is given below \n";
  echo "Response Headers are \n";
  echo $headers."\n";
  echo "Response Body \n";
  echo "$response \n";
} else {
  if($info['http_code'] == 404) {
    echo "Error, Please check the end point \n";
  } else {
    echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
    echo "Headers are ".$headers;
    echo "Response are ".$response;
  }
}
curl_close($ch);

Btw, I'm using a child theme and a custom page I added to access the include. How do I fix this?

Child theme/custompage/include

发布评论

评论列表(0)

  1. 暂无评论