我有代碼可以使用身份驗證將JSON發佈到API。我需要使用身份驗證,但大多數示例不提供此信息。GET請求與身份驗證
POST
public function putTest() {
$username = "user";
$password = "pass";
$json_url = "http://api.of.site.com";
$json_string = "[]";
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => "{$username}:{$password}",
CURLOPT_HTTPHEADER => array("Content-type: application/json"),
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
}
GET(不工作)
public function getTest() {
$username = "user";
$password = "pass";
$json_url = "http://api.of.site.com";
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => "{$username}:{$password}",
CURLOPT_HTTPHEADER => array("Content-type: application/json"),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
}
什麼我在我的GET失蹤?此外,我想保持這種風格,而不是使用curl_setopt
編輯: 作爲參考,在這裏是工作
curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" -u "username:pass" "http://api.site.name.com/thingtoget"
試過這個,我想我錯過了別的東西 – myol