Jump to content
iwato

Rendering Visible the Output of a POST Request Using cURL

Recommended Posts

GREETING: One more quick question before the weekend begins.

BACKGROUND: Until now, it has been easy to see the results of AJAX calls in the files that produce them. Until now, cURL was never an explicit part of my web-application.

I am now making a dual call, however, and am unable to see the final result.  The first call stipulates no method as nothing is sent.  The second call is based on the result of the first call and uses the POST method to make the request.  Although I can see the contents of the first request, I cannot see the contents of the second.  Both files use identical cURL routines and both perform exactly as expected, as AJAX returns everything that is requested.  Only the query statements of the URL are different.  Even the specified AJAX dataType is the same for both calls -- namely, JSON.

Please find below the code that produces the output that I wish to see, but cannot.

The PHP for the 2ND AJAX CALL

ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');
ini_set('html_errors', 0);
ini_set('display_errors', 0);
error_reporting(E_ALL);
if (!empty($_POST['ip_addr'])) {
    $ip_addr = filter_var($_POST['ip_addr'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $url = 'https://.../matomo/index.php?module=API&action=index&visitIp=' . $ip_addr . '&idSite=1&period=week&date=today&method=Live.getVisitorProfile&format=json&expanded=0&token_auth=...';
    $curl_request = curl_init();
    curl_setopt($curl_request, CURLOPT_URL, $url);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, false);
    curl_exec($curl_request);
    curl_close($curl_request);
}

QUESTION;    What must I do to make the output of the cURL call visible in the file that produces it without disrupting the AJAX call that retrieves the output that I cannot see.  Or, am I missing the whole point of cURL and the data is tranferred directly to the AJAX?  But, why would I be able to see it in the first request, but not the second. 

Roddy

Share this post


Link to post
Share on other sites

Look up the return transfer option to see what it does.  cURL outputs the response by default unless you tell it not to.

  • Like 1

Share this post


Link to post
Share on other sites

No, I already tried that.  It does not work according to the manual.  AJAX likes both of the following, but neither satisfies my desire the entire outcome en gros.

        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 0);
        curl_exec($curl_request);

        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        echo curl_exec($curl_request);

In order to iterate through the complex JSON object that results I must be able to see the whole thing JSON string!

Roddy

 

 

Share this post


Link to post
Share on other sites

You don't echo it, you store it into a variable and do some processing on it.

Share this post


Link to post
Share on other sites

I have found another way to achieve my task.  I copied the file, changed the POST variable to GET and accessed the copied file with query statement thus circumventing AJAX.  I am asking myself, however, your suggested method works.  For, if I assign the contents of the curl_exec() statement to a variable.  How will AJAX no where to get the variable's contents?

Roddy

Share this post


Link to post
Share on other sites

Your requirements were not clear. You asked to "make the output of the cURL call visible in the file that produces it" but it's not clear what "visible" means in this context. My interpretation was that you wanted the curl data visible to the script itself instead of being passed on straight to the client. The idea behind that would be that the script can manipulate the data before sending it to the client. It seems that is not what you are looking for.

Seeing that you've changed the method to GET, I suspect that you wanted to see the output in your browser by sending a GET request to the script. If your code is designed to only show content when POST data is available then you have to send POST data to see the content.

Share this post


Link to post
Share on other sites
7 hours ago, Ingolme said:

Your requirements were not clear. You asked to "make the output of the cURL call visible in the file that produces it" but it's not clear what "visible" means in this context. My interpretation was that you wanted the curl data visible to the script itself instead of being passed on straight to the client. The idea behind that would be that the script can manipulate the data before sending it to the client. It seems that is not what you are looking for. 

This is cool.  I did not know that you could do this.  I will surely keep it in mind, as it could prove very useful down the line.  I am much better at mannipulating data with PHP than I am with Javascript even when it is written in JSON.

Quote

Seeing that you've changed the method to GET, I suspect that you wanted to see the output in your browser by sending a GET request to the script. If your code is designed to only show content when POST data is available then you have to send POST data to see the content.

I do not know how to send POST data via a link.  Is it even possible?  I recall Dsonesuk teaching me how to format POST data within an AJAX call, but this is all that I can remember.

Roddy

Share this post


Link to post
Share on other sites

No, I already tried that.

You tried what?  I didn't suggest trying anything.

It does not work according to the manual.

That's not true.  Really, if one of the most-used options in cURL has a bug where it doesn't work according to the manual, people would know, and they would fix it.  I don't know what you're seeing and what you expect to see, but I bet that instead of it not working, you don't understand how it's supposed to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×