Solved

How to Figure out HTTP POST CURL Output

Posted on 2016-10-07
4
20 Views
Last Modified: 2016-10-26
Here is my script which works fine, what im trying to figure out is , how is the url constructed. Is there a way to echo what Curl is outputting to the API ?


<?php
/*** Mandatory data ***/ 
// Post URL 
$postURL = "http://mywebsite.com"; 
// The Secret key 
$secretKey = "57f4354356gfhf54"; 

/*** Optional Data ***/ 
//$firstname = "John"; 
//$lastname = "Doe"; 
//$email = "john.doe@gmail.com"; 

// prepare the data 
$data = array (); 
$data['secret_key'] = $secretKey; 
$data['slm_action'] = 'slm_check'; 
$data ['license_key'] = '5435436547frgfd';

// send data to post URL 
$ch = curl_init ($postURL); 
curl_setopt ($ch, CURLOPT_POST, true); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
$returnValue = curl_exec ($ch); 

// Process the return values 
print_r($returnValue);
?>

Open in new window

0
Comment
Question by:Ray Zuchowski
  • 2
4 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points (awarded by participants)
Comment Utility
Here's my teaching example showing how to make a cURL POST-method request.  It will find and capture the errors, if any, and will return the response object, containing the response document if the request succeeds.
<?php // demo/curl_post_example.php
/**
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 * Property 'title' is a comment field to identify the object
 *
 * http://php.net/manual/en/book.curl.php
 * http://curl.haxx.se/libcurl/c/libcurl-errors.html
 */
error_reporting(E_ALL);

Class POST_Response_Object
{
    public $href, $title, $http_code, $errno, $info, $document;

    public function __construct($href, $post_array=[], $title=NULL)
    {
        // ACTIVATE THIS TO AVOID TIMEOUT FOR LONG RUNNING SCRIPT
        // set_time_limit(10);

        // STORE THE CALL INFORMATION
        $this->href  = $href;
        $this->title = $title;

        // PREPARE THE POST STRING
        $post_string = http_build_query($post_array);

        // MAKE THE REQUEST
        if (!$this->my_curl($href, $post_string))
        {
            // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
            // trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
        }
    }

    protected function my_curl($url, $post_string, $timeout=3)
    {
        // PREPARE THE CURL CALL
        $curl = curl_init();
        curl_setopt( $curl, CURLOPT_URL,            $url           );
        curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
        curl_setopt( $curl, CURLOPT_POST,           TRUE           );
        curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
        curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
        curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
        curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

        // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
        // http://php.net/manual/en/function.curl-setopt.php#110457
        // http://php.net/manual/en/function.curl-setopt.php#115993
        // http://php.net/manual/en/function.curl-setopt.php#113754
        // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );

        // RUN THE CURL REQUEST AND GET THE RESULTS
        $this->document  = curl_exec($curl);
        $this->errno     = curl_errno($curl);
        $this->info      = curl_getinfo($curl);
        $this->http_code = $this->info['http_code'];
        curl_close($curl);

        // RETURN DOCUMENT SUCCESS SIGNAL
        return $this->document;
    }
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"] = 'Ray';
$args["mail"] = 'Ray.Paseur@Gmail.com';

// SET THE URL
$url = "http://Iconoun.com/demo/bounce_post.php";

// CREATE THE RESPONSE OBJECT
$resp = new POST_Response_Object($url, $args);

// SHOW THE RESPONSE OBJECT
var_dump($resp);

Open in new window

0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points (awarded by participants)
Comment Utility
You can't echo the output because that is done on the server and not in the browser.  There isn't any place to view it.  curl constructs a standard POST request just like a browser does.  I converted your code above into a standard HTML form and captured the request headers.  It looks like this:
http://10.202.46.40/dibtest/postdump.php

POST /dibtest/postdump.php HTTP/1.1
Host: 10.202.46.40
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://10.202.46.40/dibtest/POST-Submit-Test.html
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
Content-Length: 76
secret_key=57f4354356gfhf54&slm_action=slm_check&license_key=5435436547frgfd
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Fri, 07 Oct 2016 21:24:32 GMT
Connection: close
Content-Type: text/html
X-Powered-By: ASP.NET, PHP/5.3.28
----------------------------------------------------------

Open in new window

0
 

Author Comment

by:Ray Zuchowski
Comment Utility
Awesome thank you
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Both good and appropriate comments.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now