Home > Backend Development > PHP Problem > How to use api in php

How to use api in php

王林
Release: 2023-02-26 20:36:01
Original
2700 people have browsed it

How to use api in php

1. Use the file_get_contents() function

$params = array('key' => '8d284859d04cfeeea6b0771f754adb49',
   'location' => $_COOKIE["lng"].",".$_COOKIE["lat"]);

$url3 = "http://restapi.amap.com/v3/geocode/regeo";

if (strripos('?', $url3)) {
 $url4 = $url3 . http_build_query($params);
} else {
 $url4 = $url3 . '?' . http_build_query($params);
}

$ret2 = json_decode(file_get_contents($url4));

var_dump($ret);
Copy after login

2. Use PHP curl expansion

$url2 = 'http://www.sojson.com/open/api/weather/json.shtml';    
$params = array('city' => '长沙');   
if (strripos('?', $url2)) {
    $url = $url2 . http_build_query($params);
     }
else {
    $url = $url2 . '?' . http_build_query($params);
     }
     $data = 'city=长沙';
     $curlobj = curl_init();   
     curl_setopt($curlobj, CURLOPT_URL, $url); 
     curl_setopt($curlobj, CURLOPT_HEADER, 0);
     curl_exec($curlobj);
Copy after login

Recommended tutorial: PHP video tutorial

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

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