ajax
  • curl
  • guzzle
  • 2017-04-05 10 views 0 likes 
    0

    私はcURLでAPIに接続しようとしています。私はajaxを使用すると動作しますが、LaravelでguzzleHTTPを使用しようとしたり、単純なcURLを使用しようとすると "403禁止"となります。ajaxをcURLに変換する

    これはAjaxコードである:

    body = '{"qualifier": "registration\/registerUser", "data": {"registrationInfo": {"registrationType": "Lead", "email": "[email protected]", "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 この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" => "[email protected]", 
           "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

    どうすれば問題を解決できますか?

    +0

    のためにそれをチェックし、あなたが 'ajax'を使用するときに、自動的に送信されたセッションクッキーがあり、他の-の方法ではない –

    +0

    @YuJiaaoはあなたに感謝します。あなたが気にしないならば、あなたは解決策を提供することができます。 – tilmido

    答えて

    0

    このプロセスはログインプロセスを真似しています。最初にログインし、CookieJarにCookie /トークン/セッションを保存してから、以下のような他の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(); 
    
    関連する問題