Showing posts with label response. Show all posts
Showing posts with label response. Show all posts

Friday, July 5, 2013

CURL Requests With PHP

Most developers prefer to use HTTP Request / Response service in their projects. In this situation, you have to send data as POST method to the opponent.

Imagine that you are a web master of your own e-commerce web site. Members of the site use coupon during check out. In these conditions, you may connect to other systems, for example the store which is in another sector, and have to validate if the coupon is correct or something like that. Here is the magnificent specimen of pure one of the best example in the world for this article :)

If you want, let's code CURL for now!

For this, I set a service up for posting data: service.php
if(isset($_POST['field'])) {
    print "Field is: ".$_POST['field'];
else {
    print "Field is blank!";    
Like you've just seen above, the service is waiting for field post variable on it. If you send a field data, the screen is going to be like,

Field is your field value
But else,

Field is blank!
I suppose to send a field value to the service: myfile.php

//display error
ini_set('display_errors', 1);
$ch = curl_init("http://localhost/CURL/service.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
If I run the myfile.php page, just going to see on on the screen like,

Field is
For this example, I used a page which was http protocol. But sometimes I need use to https. In these conditions,have to add this line on it,

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
There are so many settings and options in CURL. If you want to check it out, you can visit PHP official page, here

See you next articles!