Home  >  Article  >  Backend Development  >  How to use api in php

How to use api in php

王林
王林Original
2019-10-16 11:46:452647browse

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);

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);

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!

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