0

Hi I need to find a tutorial that will show me how to post credit card info from a php mysql website that I am building.

The credit card processor wants the data in a jason file so I need to learn how to encode the data to a jason file and then post it to their API.

the encoding is easy but there is no info on how to post the jason file to the API from my php file.

Can anyone point me in the right direction. This is first time I am not able to find a how to on the web.

Thanks Jon

Jon
  • 1
  • 2
  • I strongly recommend you use Guzzle (a very popular and widely used PHP library for making HTTP requests more easily) for this, but I've linked to both a Guzzle and a non-Guzzle solution. – ceejayoz Feb 10 '19 at 15:30
  • Thanks but I don't see the link. Thanks – Jon Feb 12 '19 at 03:44
  • Look in the yellow box up top titled "This question already has an answer here:" – ceejayoz Feb 12 '19 at 14:09

1 Answers1

0

I think the reason you're having such a hard time is because you're probably referring to JSON, not jason.

The best way to do this would be as follows:

$data = array("name" => "NameOnCard", "address" => "Address", "number" => "CardNumber" );                                                                    
$json = json_encode($data);                                                                                   

Then you can easily send $json as the body of your post.

$ch = curl_init('https://example_api.com');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($json))                                                                       
);  
$response = curl_exec($ch);
curl_close ($ch);
LucyTurtle
  • 1,043
  • 1
  • 17
  • 37
  • Hi Lucy Thanks so much for your help on this. – Jon Feb 10 '19 at 01:05
  • Since I am still learning php I am confused about the exact steps from the point of the user clicking the submit button and the JSON file being submitted to the credit processing API. I am assuming and please coach me on this that I send form data to a .php page and then process the data to a json file with the json_encode() but how does this json data get sent to the API. Is there a php command that is automaticly doing that? Also do I need to use curl? Is curl somthing I need to download? can I download it as a package to ATOM? Thank you so much for the help. Jon – Jon Feb 10 '19 at 01:15
  • Jon, to really go over this we would need to continue this in a chat of some sort. You're not really supposed to use the comments as a way to carry on with questioning and answering so the best way to get to all your questions would be in a chat. I'm not active in any of the stack overflow chats so I started a slack: https://join.slack.com/t/stackoverflowhelp/shared_invite/enQtNTQ2NDU4MDM2MDE3LTA4Yjk0ZjQ0MGM3OWY4OTY5NmQwYmJkNDQ2ZTQ3YmEzOTM2NDAzNzI3NDQ0OWJjMGQ1ZGVlYzZhYzE5OTJkMGI – LucyTurtle Feb 10 '19 at 15:29
  • Hi Lucy I would love to do that but first I want set it up and get a far as possible so that I have my question ready for you. Do you charge people to do this? I should be ready on Wed. Thanks Jon – Jon Feb 12 '19 at 03:42
  • Jon, nono! No charge I just want to help. (: I'll make sure to have the notifications on and check the slack on weds – LucyTurtle Feb 12 '19 at 14:55