How PHP handles Java-coming interfaces and the JSON string that PHP returns to JavaScript

Source: Internet
Author: User
Tags header json

If you want to handle Java interface. This is how it behaves in PHP:

<?php  
    $url = ' The interface URL address for Java provided here ';//We are unified using JSON format  
    $ch = Curl_init ();  
    $timeout = 5;  
    curl_setopt ($ch, Curlopt_url, $url);  
    curl_setopt ($ch, Curlopt_returntransfer, 1);  
    curl_setopt ($ch, Curlopt_post, 1);  
    curl_setopt ($ch, Curlopt_connecttimeout, $timeout);  
    $contents = curl_exec ($ch);  
    Curl_close ($ch);  
    $body = Json_decode ($contents, TRUE);  
    $data = Array ();  
    if ($body [' code '] = = =  
        $data = (array) $body [' Data '];  
>

The following is a JSON string that has been processed to return to JavaScript. You just need to put the address of the controller in the form of a URL to the docking personnel can be.

<?php Public  
function Action_actionname ()  
{  
    header (' Content-type:application/x-javascript ');  
    For example, the request  
    header (' access-control-allow-origin:.baidu.com ') that receives only the. baidu.com domain name;  
    $callback = Isset ($_get[' callback '))? $_get[' callback ']: ' Callback ';  
    try {  
        $rows = select * from *******;//Here is a SELECT statement that extracts results from the database  
        $data = array (  
            ' code ' =>,  
            ' msg ' 
  
   => ' success ',  
            ' data ' => $rows,  
        );  
    } catch (Exception $e) {  
        $data = array (  
            ' code '  => 3000004,  
            ' msg '   => ' illegal operation ',  
        );  
    }  
    echo $callback. ' ('. Json_encode ($data). ') ';  
    Exit;  
}  
        
? >
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.