Skip to main content

Today we will look into how to implement cUrl in php application

Passing json file via cUrl in php

We will implement passing json file via cUrl and submit to next server. cUrl is all about special communication between two or more server. Such as server1.com sending data to server2.com. A lot of beginner had always ask endless question about what is cUrl and what cUrl actually does. A simple answer there is: cUrl is a special communication between two or more server. Now, if you look at json code below, it has been neatly written and indented and ready for testing. Launch your notepad copy the json data below and save it as data.json place it in a folder where you saved other php file.

{ "id": "1", "image": "image\/chiquado.jpg", "fullname": "Kenan Chiquado", "fulladdress": "Udah, Igbo-eze North LGA, Enugu state, Nigeria", "emailaddress": "2348130802790", "phonenumber": "presschiquado@gmail.com", "username": "Chikwado", "password": "web@design&33", "project": "Local and public network developer", "subscription": "0.00", "gender": "Male", "reg_date": "2023-03-27 10:18:48" }

Php passing data from server.com to server.com using cUrl

In the php code below, it will send data from server1.com to server2.com. First, it connect server2.com, get json file, decode it, store it in php array and send it to server2.com, server2.com will process the data and print it out. Now, copy the below code and save it as index.php in the same folder where you saved the data.json file

<!--php $url = 'http://localhost/curl/curl/view.php'; $curl = curl_init(); $data = file_get_contents("data.json"); $prof = json_decode($data, true); $id = $prof['id']; $image = $prof['image']; $fullname = $prof['fullname']; $fulladdress = $prof['fulladdress']; $emailaddress = $prof['emailaddress']; $phonenumber = $prof['phonenumber']; $username = $prof['username']; $password = $prof['password']; $project = $prof['project']; $subscription = $prof['subscription']; $gender = $prof['gender']; $reg_date = $prof['reg_date']; $fields = array( 'id' =--> $id, 'image' => $image, 'fullname' => $fullname, 'fulladdress' => $fulladdress, 'emailaddress' => $emailaddress, 'phonenumber' => $phonenumber, 'username' => $username, 'password' => $password, 'project' => $project, 'subscription' => $subscription, 'gender' => $gender, 'reg_date' => $reg_date ); $fields_string = http_build_query($fields); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string); curl_exec($curl); curl_close($curl); ?>

Php script to receive a request coming server1.com using cUrl

In below php code, this code should be placed in server2.com. When a request is coming from server1.com this php script will receive the request and process the data and print it to the stream. Now, copy the php code below and place it in server2.com for testing. Then type the url from server1.com which will trigger and process the php script and return the output.

<!--php $id = $_POST["id"]; $image = $_POST["image"]; $fullname = $_POST["fullname"]; $fulladdress = $_POST["fulladdress"]; $emailaddress = $_POST["emailaddress"]; $phonenumber = $_POST["phonenumber"]; $username = $_POST["username"]; $password = $_POST["password"]; $project = $_POST["project"]; $subscription = $_POST["subscription"]; $gender = $_POST["gender"]; $reg_date = $_POST["reg_date"]; echo "<p-->".$id."</p>"; echo "<p>".$image."</p>"; echo "<p>".$fullname."</p>"; echo "<p>".$fulladdress."</p>"; echo "<p>".$emailaddress."</p>"; echo "<p>".$phonenumber."</p>"; echo "<p>".$username."</p>"; echo "<p>".$password."</p>"; echo "<p>".$project."</p>"; echo "<p>".$subscription."</p>"; echo "<p>".$gender."</p>"; echo "<p>".$reg_date."</p>"; ?>

Summary

While testing the above script, if you have no access to two server you can test it in same server. You can place the second php code in deferent folder. If everything goes well it will run and return the output. If you encounter error messages from php, don't hesitate to get in touch, we are open to respond to your question.

Comments

Latest post

We will take a closer look on how to bind webpage in python programming language

Today we will take a closer look on how to run python in your webpage under windows operating system. Let's assume you have installed xampp in your windows operating system and have python program installed as well. If you have not done so, then go on to installed xampp in your computer together with python program. Remember to select software version compactible to your computer. When you are ready with these two software, then copy the code below: #!"C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\python.exe" print ("Content-Type: text/html\n") print (" ") print (" ") print (" ") print (" ") print (" Hello world! ") print (" Welcome to my first python webpage ") print (" ") print (" ") Save the above code as index.py in C:\xampp\htdocs the path should be: C:\xampp\htdocs\index.py On your browser address bar you can type http://localhost/index.py, ...

Our today post will focus on how to implement php class and method

A class is a container for objects, and object is items in class. Class is container that we can store all kinds of obejects, such as variable, function etc. Today we will implement class and objects , property, set, get and the rest. If you look at the php class below, this is exactly how to declare empty php class. Php class container with public objects, function set parameters and gets parameters. In php class below, we created two functions, one to set a value and one to get the value, pass the instance object and its value. Now you can copy the below code for your testing. name = $name; } function getname() { return $this->name; } } $username = new KcFuns(); $username->setname('Kenan Chiquado'); echo "Output:"; echo "Username is: ".$username->getname(); echo "----------------------------------"; ?> Php class several objects function and parameters In our second php class implementation, we will lo...

Our today post will focus on how to implement php hits counter

PHP counter step 1: Have you ever wonder how you can monitor, view or learn how many times your website is been processed by visitors. How many times your website is been browse by your visitors? If your answer is 'no', then it's time to learn all these. You can learn how many times browser processed your website by binding certain php script on top of your webpage. Now, if you look at our php script below, this script will help you count how many times your website was processed at any given moment. To apply our php script below, you can copy and paste this script on top of your webpage before html declaration. PHP counter step 2: Again, the below script, you can copy and put it in a separate file. All you need to do is check time to time and see the digits number your script return. That's exactly how many times your website was processed. Example of the counter: Hits counter While applying our tutorials, if our code spot error, don't hesit...

In today post we will focus on how to create php script that could upload picture to server

Before writing script to upload files to server, you have to check ensure that php file upload is set to 'On' in php.ini. Open C:\xampp\php\php.ini file locate the line that says upload_file = On and also change the php upload max setting, example: php_max_upload = 100m. Now save the file, restart the server and start writing php script to upload your file. Step 1: Below is the html code to implement our practice of file upload. You can write it manually or copy and paste. " enctype="multipart/form-data" method="POST"> Uploader Step 2: Php command to target the upload folder, php comman to get file name, php command to check upload status, and php command to validate file extension. Below is our sample code: Step 3: We will use php global object called $_POST to get the image object. And check if the file is image or not, if it's not image the upload status will be set to zero. Below is our sample code: if(isset(...