将ajax转换为cURL

时间:2017-04-05 00:49:16

标签: ajax curl guzzle

我正在尝试通过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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

该过程模仿登录过程,首先登录,将cookie /令牌/会话存储到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();