簡體   English   中英

PHP CURL GET 請求返回 401 錯誤(身份驗證失敗)

[英]PHP CURL GET request return 401 error (Auth fail)

首先,我發送一個帶有登錄名和密碼(管理員授權)的 POST 請求並獲取一些字段。

接下來,我需要向 API 發送 GET 請求(檢查客戶端電子郵件是否存在),但我收到 CURL 錯誤:請求的 URL 返回錯誤:401

在 Postman GET 請求中,我做錯了什么?

這是我的 GET 請求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, "www.myurl.com/clients?email=a@a.a");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);

如果 GET,您必須傳遞查詢字符串。 試試下面的更新代碼。

$qry_str = "?email=a@a.a"
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'www.myurl.com/clients.php',$qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM