DeveloperBreeze

cURL Login with Cookie Extraction

$url = 'https://example.com/api/login';

# Initialize cURL session
$curlObj = curl_init();

# Set cURL options
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, 'email=email&password=password');
curl_setopt($curlObj, CURLOPT_URL, $url);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_HEADER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false);

# Execute cURL session
$result = curl_exec($curlObj);

# Close cURL session
curl_close($curlObj);

# Extract cookies from the response headers
preg_match_all('/^Set-Cookie:\\s*([^;]*)/mi', $result, $match_found);
$cookies = array();
foreach ($match_found[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}

# Display extracted cookies
print_r($cookies);

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
php

Integrating and Using NMI Payment Gateway in Laravel

Inside the NMI class, add the following method:

   public function addCustomerVault($creditCard)
   {
       $data = [
           'ccnumber' => $creditCard['cc_number'],
           'ccexp' => $creditCard['exp_date'],
           'cvv' => $creditCard['cvv'],
           'customer_vault' => 'add_customer',
           'security_key' => $this->securityKey,
       ];

       if ($this->production === false) {
           $data['test_mode'] = 'enabled';
       }

       $createVaultCurl = curl_init();
       curl_setopt_array($createVaultCurl, [
           CURLOPT_URL => $this->url,
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_POST => true,
           CURLOPT_POSTFIELDS => http_build_query($data),
           CURLOPT_SSL_VERIFYHOST => false,
           CURLOPT_SSL_VERIFYPEER => false,
       ]);
       $createVaultResponse = curl_exec($createVaultCurl);
       curl_close($createVaultCurl);

       \Log::info('NMI Vault Response: ' . $createVaultResponse);

       if ($createVaultResponse === false) {
           return [
               'responsetext' => 'error',
               'error' => 'Customer vault creation failed',
           ];
       }

       parse_str($createVaultResponse, $vaultResponseArray);
       return isset($vaultResponseArray['customer_vault_id']) ? $vaultResponseArray['customer_vault_id'] : null;
   }

Aug 14, 2024
Read More
Tutorial
python

Creating a Simple REST API with Flask

  source venv/bin/activate

Create a file named app.py and add:

Aug 03, 2024
Read More
Code
bash

Various cURL Examples for API Interactions

# Update resource with PUT request
curl -X PUT https://api.example.com/resource/123 \
    -H 'Content-Type: application/json' \
    -d '{"key": "updated_value"}'

# Retrieve data with GET request
curl https://api.example.com/data

# Send data with POST request to a form endpoint
curl -X POST https://api.example.com/form-endpoint \
    -d 'param1=value1&param2=value2'

# Delete resource with DELETE request and Authorization header
curl -X DELETE https://api.example.com/resource/456 \
    -H 'Authorization: Bearer your_access_token'

# Upload a file with POST request to an upload endpoint
curl -X POST https://api.example.com/upload-endpoint \
    -H 'Content-Type: multipart/form-data' \
    -F 'file=@/path/to/your/file.txt'

# Send JSON data with POST request to a secured endpoint with headers and authorization
curl -X POST https://api.example.com/post-endpoint \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer your_access_token' \
    -d '{"username": "john_doe", "password": "secretpassword"}'

# Send JSON data with POST request to an endpoint with cookies
curl -X POST https://example.com/api/endpoint \
    -H 'Content-Type: application/json' \
    -H 'Cookie: sessionid=your_session_id' \
    -d '{"key1": "value1", "key2": "value2"}'

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!