Notes and precautions for calling the interface on PC, precautions in late pregnancy, precautions when flying, precautions after miscarriage

WBOY
Release: 2016-07-29 08:54:17
Original
1252 people have browsed it

data-id="1190000004902725">

Preface

Good habits create a good life, and you must be good at summarizing during development. Today I will continue to share some useful information with you. Fans who follow me will benefit from it. Below are some notes I compiled when calling the interface on the PC, as well as reminders of things you need to pay attention to!

Cause Analysis

1. First, let me talk about why the interface is called on the PC side to obtain data!
I’ll give you a link: http://www.bitscn.com/pdb/php/201411/402…. After reading this article, you will probably understand what I mean.
2. Integrating relevant information is not only conducive to the acquisition of information, but also serves as a lesson for others. People who have planted trees can benefit future generations, right? Haha, I am a philanthropist.

Note organization

1. Three ways for Yii2 PC to call the interface to obtain data

PHP method to call the Api interface

<code>    1、直接在方法里引用接口的url。
    2、通过file_get_contents()函数获取url的数据。
    3、把获取到的JSON格式数据进行反转。(可选)
    4、参考网址:http://www.jb51.net/article/20705.htm   //PHP远程调用URL
        例: $url='http://api.xxx.com/v1/departments?id=list&company_id=1';
             $data=file_get_contents($url);
             $data_1 = json_decode($data,true);     //JSON反转</code>
Copy after login

Ajax method to call the Api interface

<code>    例:
        $.ajax({
        type:"POST",
        url: //你的请求程序页面随便啦(接口地址)
        async:false,//同步:意思是当有返回值以后才会进行后面的js程序。
        data://请求需要发送的处理数据
        success:function(msg){
            if (msg) {//根据返回值进行跳转
                window.location.href = '你的跳转的目标地址(页面地址)';
            }
        }</code>
Copy after login

JQ method to call the Api interface

<code>例:
        <script type="text/javascript" src="/apihandonesvn/frontend/web/assets/68738eee/jquery-1.11.2.min.js"></script>
        <script type="text/javascript">
            //1、GET方式
            $.get('http://api.XXX.com/v1/departments?grade=1',function(data){ 
                    //  console.log(data);//输出内容,类似alert()
                     $('#content').html(data);
            });

            //2、POST方式
            $.post('http://api.XXX.com/v1/departments?grade=1',{a:1,b:2,c:3},function(data){ 
                     $('#content').html(JSON.stringify(data));
            });

        </script></code>
Copy after login

Added: If you use the latter two methods, add the following code at the top of all methods of the controller corresponding to the interface

<code> public function behaviors()
    {
        return ArrayHelper::merge([
            [
                 'class' => Cors::className(),
                 'cors' => [
                     'Origin' => ['http://www.ceshi.com'],//PC端的Url
                     'Access-Control-Request-Method' => ['GET','POST','PUT','DELETE', 'HEAD', 'OPTIONS'],
                 ],

                'actions' => [
                    'index' => [
                        'Access-Control-Allow-Credentials' => true,
                    ]
                ]
            ],
        ],
            parent::behaviors());
    }</code>
Copy after login

The above three methods of calling the interface on the PC side have been tested by me and are all feasible. You can choose what you like.

2. When calling the interface on the PC, how does the interface obtain the uid?
At this time, the interface cannot be obtained using Yii:$app->user->id that comes with Yii, because it is impossible to log in through the interface. To obtain the uid of the current logged-in user through the interface, you can pass an access-token through the PC, and then use get on the interface to find out the uid and solve the problem.
This method can also be imitated when the interface obtains other parameters.

3. Report: PHP Warning – yiibaseErrorException
Invalid argument supplied for foreach() error problem and solution
This error is caused by looping empty data. As long as a judgment must be added before the data is looped to ensure that the data exists before the loop can be looped. solved. Although this is not a particularly difficult error to solve, we still have to pay attention to details, as details determine success or failure.

Reminder

1. The PC calls the interface for local testing. It is best not to match the local interface address with the Internet, because then it will go to the local interface first. If the local interface is good, it is difficult to find the reason.

Related information

PHP (CURL) POST data calling API simple example: http://eyexiaobo.iteye.com/blog/1100712

The above introduces the notes and precautions for calling the interface on the PC side, including precautions and interface content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template