Data-id= "1190000004902725" >
Preface
Good habits create good life, development should be good at summing up. Today I continue to share some of the dry goods, the attention of my fans have welfare ha. Below is my PC-side call interface to sort out some of the notes, as well as remind everyone to pay attention to matters!
Cause analysis
1. First I say why the PC-side call interface to get data it!
Give you a link: http://www.bitscn.com/pdb/php/201411/402 .... After reading this article, I guess you will understand my intentions.
2, the relevant information integration, not only conducive to the acquisition of information can also be used as a precedent, walnuts posterity understand it? Haha, I am the fraternity party.
Notes collation
1, Yii2 PC-side call interface to obtain data of three ways
How PHP calls the API interface
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反转
Ajax methods for invoking API interfaces
例: $.ajax({ type:"POST", url: //你的请求程序页面随便啦(接口地址) async:false,//同步:意思是当有返回值以后才会进行后面的js程序。 data://请求需要发送的处理数据 success:function(msg){ if (msg) {//根据返回值进行跳转 window.location.href = '你的跳转的目标地址(页面地址)'; } }
How JQ invokes the API interface
例:
Add: Using the latter two methods, the interface corresponding to the controller at the top of all the methods to add the following 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()); }
The above three kinds of PC-side call Interface way, I have tested all feasible, you each take love.
2, the PC-side call interface, interface how to obtain the UID problem?
At this point the interface is not available with Yii's Yii: $app->user->id is obtained because the interface is not possible to log in. interface to get the UID of the current login can pass a access-token on the PC side, and then in the interface with get get to find out the UID, solve.
This method can also be imitated when the interface obtains other parameters.
3, newspaper: PHP warning–yii\base\errorexception
Invalid argument supplied for foreach () Error problem and workaround
This error is caused by the circulation of empty data, as long as the data loop before the need to add a judgment to ensure that the data exists in order to solve the cycle. This is not a particularly difficult mistake to solve, but we have to pay attention to the details, the details determine success or failure.
Alert note
1, the PC-side call interface for local testing, the local interface address is best not the same as the Internet, because it will first go local interface, if you encounter the local interface good, not to find the reason.
Related information
PHP (CURL) Post data Call API Simple example: http://eyexiaobo.iteye.com/blog/1100712
The above describes the PC-side call interface notes collation and considerations, including considerations, interface aspects of the content, I hope that the PHP tutorial interested friends helpful.