【问题标题】:Convert ajax to cURL将 ajax 转换为 cURL
【发布时间】:2017-04-05 20:13:37
【问题描述】:

我正在尝试通过 cURL 连接到 API。当我使用 ajax 时,它可以工作,但是当我尝试在 Laravel 中使用 guzzleHTTP 或使用简单的 cURL 时,我得到“403 禁止”。

这是ajax代码:

body = '{"qualifier": "registration\/registerUser", "data": {"registrationInfo": {"registrationType": "Lead", "email": "email@gmail.com", "firstName": "ly", "lastName": "developer test", "telephone": "+41-555-1234567", "countryCode": "zd"}, "marketingInfo": {"adServer": "myAdServer", "adData": "myAdData", "utmCampaign": "myUtmCampaign", "utmContent": "myUtmContent", "utmTerm": "myUtmTerm", "utmSource": "myUtmSource", "utmMedium": "myUtmMedium", "affiliateId": 36293}}, "context": {"apiKey": "dsfcvxmlkddvmdsdmlgkgdsmkgsdflksd", "apiSecret": "lkmlmlk", "locale": "en-US"}}';
$(document).ready(function () {
    $("#myclick").click(function () {
        $.ajax({
            url: 'https://example.com',
            type: 'post',
            data: body,
            success: function (data, textStatus, jqXHR) {
                console.log(data);
            }
        });
    });
});

==== 更新 这是 laravel 中的 php 代码

thank you @pang, plz can you provide a solution for that. this is code in php `
public function sendToNet() {
    // Provide the body as a string. 
    $body = json_encode([
        "qualifier" => "v1/registration/registerUser",
        "data" => [
            "registrationInfo" => [
                "registrationType" => "Lead",
                "email" => "test04052017001@email.com",
                "firstName" => "firstName",
                "lastName" => "vlastName",
                "telephone" => "+962-4-123456",
                "countryCode" => "SA"],
            "marketingInfo" => [
                "adServer" => "adServerData",
                "adData" => "adData",
                "utmCampaign" => "utmCampaign",
                "utmContent" => "utmContent",
                "utmSource" => "utmSource",
                "utmTerm" => "utmTerm",
                "utmMedium" => "utmMedium",
                "affiliateId" => "45345"
            ]
        ],
        "context" => [
            "locale" => "ar-SA",
            "apiKey" => "dmflkdsfmldskdsgksmlkgkslkgdsmlk",
            "apiSecret" => "mlkmlkmlk"]
    ]);

    $Client = new Client();
    $res = $Client->request("POST", 'https://example.com', [
                'json' => $body
            ])->getBody();
    dd($res);
    try {

    } catch (\GuzzleHttp\Exception\ClientException $e) {
        dd($e);
    }
}

这就是我得到的结果: result image

我该如何解决这个问题?

【问题讨论】:

  • 当您使用ajax时,会自动发送会话cookie,其他方式不会
  • @YuJiaao 谢谢。如果您不介意,请提供解决方案。

标签: ajax curl guzzle


【解决方案1】:

这个过程是模仿登录的过程,先登录,把cookie/token/session存到CookieJar,然后用它来处理其他的URL请求,如下图,是个例子说明思路,我没查过它是为了正确

use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;

$cookiePlugin = new CookiePlugin(new ArrayCookieJar());

// Login first
$client = new Client();
$client->addSubscriber($cookiePlugin);
$client->post("http://www.test.com/authenticate",null, array(
"username" => 'yourname',
"password" => 'AndYourPass', 
))

// Send the request with no cookies and parse the returned cookies
$client->get('http://www.test.com/other/staff')->send();

【讨论】:

    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 2015-10-06
    • 2018-02-15
    • 2016-10-10
    • 2021-02-14
    • 2017-05-18
    • 1970-01-01
    相关资源
    最近更新 更多