php - 使用 SSL 和 AUTH 的 cURL POST 到 Laravel PHP API

标签 php curl ssl laravel laravel-4

我一直在为我的 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/

相关文章:

ssl - 无法通过使用 MSXML2.ServerXMLHTTP 的 SHA2 证书的 HTTPS 连接到服务器

PHP PDO 和 IN 子句

php - 当您没有该功能的指定模型时,将业务逻辑放在哪里?拉维尔

php - 将动态生成的表单嵌入到另一个表单中

PHP Curl,请求数据返回application/json

php - str_get_html 未加载有效的 html 字符串

php - 从 URL 下载图像并上传到 WordPress 媒体库

swift - 我想通过 Swift 实现 `curl --header $1`。(NSURLSession 的标题)

ssl - 由于 SSL 问题,Curl to Azure devops 失败

android - 如何修复 nginx 中不完整的证书链