我正在尝试使用PHP访问API,但教程仅用Python编写。本教程展示了如何使用从URL检索数据
res=请求.get(API_URL,auth=(UID,SECRET))
请有人告诉我PHP中的等效语句是什么,谢谢。
<?php
$UID = "UserID";
$SECRET = "Secret";
echo file_get_contents('http://apiurl.com/?UID='.$UID.'&SECRET='.$SECRET);
?>
因此,实际上现在file_get_contents的URL将变为:http://apiurl.com/?UID=UserID&SECRET=机密
您可以使用file_get_contents,如下所示:
<?php echo file_get_contents('http://apiurl.com'); ?>
或者这个卷曲函数:
<?php
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_get_contents('http://apiurl.com');
?>