[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 56
  • Last Modified:

How to Figure out HTTP POST CURL Output

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
Ray Zuchowski
Asked:
Ray Zuchowski
  • 2
2 Solutions
 
Ray PaseurCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray ZuchowskiInformation Systems ManagerAuthor Commented:
Awesome thank you
0
 
Dave BaldwinFixer of ProblemsCommented:
Both good and appropriate comments.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now