weixin_33694620 2017-04-05 00:49 采纳率: 0%
浏览 122

将ajax转换为cURL

I am trying to connect to an API by cURL. When I use ajax, it works, but when I try to use guzzleHTTP in Laravel or use simple cURL, I get "403 forbidden".

This is the ajax code:

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);
            }
        });
    });
});

==== UPDATE this is code in php in laravel

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);
    }
}

and this is what i get in result: result image

How can I solve the problem?

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33728708 2017-04-05 12:13
    关注

    The process is imitate the login process, first login, store the cookie/token/session to the CookieJar, then use it to other URL requests, like below, it is a example to show the idea, I haven't checked it for correctness

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题