Home  >  Article  >  Backend Development  >  How to access the interface in php

How to access the interface in php

(*-*)浩
(*-*)浩Original
2019-10-17 10:14:203857browse

You can call it by simulating post request through php.

How to access the interface in php

php Method to simulate POST submission:

In order to use PHP’s cURL function, You need to install the libcurl package. (Recommended learning: PHP video tutorial)

libcurl currently supports http, https, ftp, gopher, telnet, dict, file and ldap protocols. libcurl also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP upload (this can also be completed through PHP's FTP extension), HTTP form-based upload, proxy, cookies and username and password authentication.

Php code:

$post_data = array(); 
$post_data['clientname'] = "test08"; 
$post_data['clientpasswd'] = "test08"; 
$post_data['submit'] = "submit"; 
$url='http://xxx.xxx.xxx.xx/xx/xxx/top.php'; 
$o=""; 
foreach ($post_data as $k=>$v) 
{ 
$o.= "$k=".urlencode($v)."&"; 
} 
$post_data=substr($o,0,-1); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_URL,$url); 
//为了支持cookie 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
$result = curl_exec($ch);

The above is the detailed content of How to access the interface in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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