Home > database >  Having troubles for using CURL and API using PHP
Having troubles for using CURL and API using PHP

Time:05-13

Good day family ! Please help me using this API. This API is user to get a token see the normal answer : {'status': True, 'message': 'Success', 'code': 50, 'data': {'token': '773b718a53341b57zd511dazd4588fdf2', 'amount': 45.0}}

When i use Python it's working fine ! here is the python working code :

import requests
import json

headers = {"Authorization": "Token 12345678900987654321234567890", "Content-Type":"application/json"}
data = {"vendor": 1265,
        "amount": 45,
        "note" : "Commande #1324"
    }
r = requests.post("https://sandbox.paymee.tn/api/v1/payments/create", data=json.dumps(data), headers=headers)
response = r.json()
# Now you can use 
print(response)

But when i use PHP i have as output : NULL Here is the code

<?php
$headers = array("Authorization: Token 12345678900987654321234567890");
$curl_post_data = array(
    'vendor' => 1265,
    'amount' => 45,
    'note' => "Commande #1324"
);
$curl_handle=curl_init('https://sandbox.paymee.tn/api/v1/payments/create');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_se

topt($curl_handle, CURLOPT_POSTFIELDS, $curl_post_data);

$query = json_decode(curl_exec($curl_handle),true);

 var_dump($query); 
curl_close($curl_handle);

?>

Do you have any solution please ? i spent one week stuking there ! and my exam is close... :(

Thanks in advance

CodePudding user response:

$result = curl_exec($curl_handle);
$query = json_decode($result,true);

CodePudding user response:

<?php
$headers = array(
    "Authorization: Token 12345678900987654321234567890",
    "Content-Type: application/json"
);
$data = array(
    "vendor" => 1265,
    "amount" => 45,
    "note" => "Commande #1324"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sandbox.paymee.tn/api/v1/payments/create");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);

curl_close($ch);
$response = json_decode($response, true);
print_r($response);

?>

This gives me the response that I'm using a invalid token which seems about right since you gave a sample token.

  • Related