简体   繁体   English

具有SSL和AUTH的cURL POST到Laravel PHP API

[英]cURL POST with SSL and AUTH to Laravel PHP API

I have been writing a RESTful API for my Laravel application. 我一直在为Laravel应用程序编写RESTful API。 I can submit data using cURL in Terminal with the following line; 我可以在Terminal中使用cURL通过以下行提交数据;

curl -i --user admin@admin.com:qwertyuiop -d "data=somedata" https://www.xxxxxxxxxxxxx.co.uk/app/api/v1/clients

but when i try and do the same in cURL using PHP it gives me a 403 forbidden error. 但是,当我尝试使用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);

Any ideas what i'm missing from my PHP cURL script? 有什么想法我的PHP cURL脚本缺少什么? Thanks 谢谢

It's been almost 6 years since this question asked. 自问这个问题以来已经快6年了。 But I'm sure this answer will solve your and probably another problem with this. 但是我敢肯定,这个答案将解决您的问题,可能还会解决另一个问题。 This code also solved my problem. 这段代码也解决了我的问题。

$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
}

As explained in this amazing website, where I found this code about Laravel POST using cURL 如这个惊人的网站所述,我在这里找到了有关使用cURL的Laravel POST的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM