我一直在为我的 Laravel 应用程序编写一个 RESTful API。 我可以使用以下行在终端中使用 cURL 提交数据;
curl -i --user admin@admin.com:qwertyuiop -d "data=somedata" https://www.xxxxxxxxxxxxx.co.uk/app/api/v1/clients
但是当我尝试使用 PHP 在 cURL 中做同样的事情时,它给了我一个 403 禁止错误。
$url = "https://www.xxxxxxxxxx.co.uk/app/api/v1/clients";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "admin@admin.com:qwertyuiop");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=mydata");
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
知道我的 PHP cURL 脚本中缺少什么吗? 谢谢
最佳答案
问这个问题已经将近 6 年了。但我相信这个答案会解决你的问题,也可能是另一个问题。这段代码也解决了我的问题。
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requested Headers
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
dd($response); //this line is my custom code
}
正如这个令人惊叹的网站所解释的那样,我在其中找到了这段代码 Laravel POST using cURL
关于php - 使用 SSL 和 AUTH 的 cURL POST 到 Laravel PHP API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19047441/