如何使用php代码进行ajax和http请求?

I have some classes and methods which are containing some php codes. Now I want to use those PHPcodes both for ajax and http requests. How?

Should I write all my PHP codes twice? onetime for ajax requests and one time for http request?

Here is my currect structure:

/********************************************** Search.php ****/

... // some html codes

<div id="content">

<?php

if(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && 
   strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
{ 
  $type = true; // true means ajax request
} else {
  $type = false; // false means http request
}

  $obj = new classname;
  $results = obj->func($type);
  echo $results;

?>

</div>

... // some html codes



/********************************************** Classname.php ****/

class classname {

  public function func($type){
    $arr = ('key1'=>'some', 'kay2'=>'data');

    if ($type){
      echo json_encode($arr);
    } else {
      return $arr;
    }

  }

}

Now I want to know, this is standard? Actually I want to use it also for a request which came from a mobile app (something like an API). I think the above code has two problem:

  1. there will be a lot of IF-statement just for detecting type of requests (in the every methods)
  2. there is just one file (class.php) for both ajax and http requests. And when I send a ajax request by for example mobile, it will process some codes which doesn't need to them at all

Well, is there any point that I need to know?

I would first do all the business logic, calling on your classes, without any dependency on whether you are in an ajax call or not. When all is ready for output, make the switch:

Search.php:

<?php
    // first do your business logic without view dependency.
    $obj = new classname;
    $results = obj->func(); // no type is passed.

    if(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && 
       strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
    { 
        // ajax request
        echo json_encode($results);
        exit();
    } 
    // http request
?>
... // some html codes
<div id="content">
    <?=$results?>
</div>
... // some html codes

Classname.php:

<?php
class classname {
  public function func(){
    $arr = ('key1'=>'some', 'kay2'=>'data');
    return $arr;
  }
}
?>

This is more about the design of your software then simplifying it. Often you would have a structure with classes that you use and separate PHP files for types of responses.

For example:
You could have the documents: classes.php, index.php and ajax.php.

The classes would contain generic classes that can both be shown as JSON or as HTML.

PS:
Simplified could mean:

/********************************************** Search.php ****/

... // some html codes

<div id="content">

<?php

  $obj = new classname;
  $results = obj->func($_SERVER["HTTP_X_REQUESTED_WITH"]);
  echo $results;

?>

</div>

... // some html codes



/********************************************** Classname.php ****/

class classname {

  public function func($type){
    $arr = ('key1'=>'some', 'kay2'=>'data');

    if(!empty($type["HTTP_X_REQUESTED_WITH"]) && 
   strtolower($type["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest"){
      echo json_encode($arr);
    } else {
      return $arr;
    }

  }

}

You can write a single code, and you should use only partials to load AJAX data. The main reason we are using AJAX is to minimize data transfer. You should not load a full page HTML in an AJAX call, although it is perfectly valid.

I would suggest from my experience is, have a single file:

<?php
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        /* special ajax here */
        die(json_encode($data));
    } else { ?>

Regular Flow of Content

<?php } ?>