Jump to content

Need help with curl in php


yoursurrogategod

Recommended Posts

Ok, I'm new to this and I don't know much about PHP, much less Curl and how to use the functions inside of PHP:

 

http://us.php.net/manual/en/book.curl.php

 

However, I need to do something with this.  I have to create this curl.php file, inside that file I need to take the $_POST array and send it to a different php file... I'm not sure how to do this.  And yes, I only need that.  I'd post the source, but my company's proprietary policies prevent me.

Link to comment
Share on other sites

Without seeing what you've tried (if anything), it's difficult to know where you're at.  For example, are you at least managing to capture the post data in your proposed curl.php file before sending it to the other php file?

 

There's a decent tutorial here that covers posting to another URL using cURL.  Have a look at the aptly named "POSTing to a URL" section.

 

That should at least help you out with what I'd say is the more difficult part of your problem.

Link to comment
Share on other sites

Without seeing what you've tried (if anything), it's difficult to know where you're at.  For example, are you at least managing to capture the post data in your proposed curl.php file before sending it to the other php file?

 

There's a decent tutorial here that covers posting to another URL using cURL.  Have a look at the aptly named "POSTing to a URL" section.

 

That should at least help you out with what I'd say is the more difficult part of your problem.

Thanks, that really helps.

Link to comment
Share on other sites

Running into an issue.  This is my code:

<?php
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_POST, "http://webstage.intra.lifespan.org/applications/inquiz/index.php?survey=custom-cme-modsedation");

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);


echo "<p>The survey is complete. Thank you for your time.</p>";

echo "<a href=\"http://www.surveymonkey.com/s/G22Y9TR\">Survey 
Evaluation</a>";
?>

Here is the issue, those values don't seem to be getting passed along.  Again, curl + php n00b, I'm probably doing something silly :) .

Link to comment
Share on other sites

<?php
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://webstage.intra.lifespan.org/applications/inquiz/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

echo "<p>The survey is complete. Thank you for your time.</p>";

echo "<a href=\"http://www.surveymonkey.com/s/G22Y9TR\">Survey Evaluation</a>";
?>

Oh man, I'm close!  Ok, the above does what I want... well... almost what I want.  The $_POST array gets sent across swimmingly.  The only problem that I have is that once that data gets sent across, the page (index.php) is displayed in my curl.php (at the bottom of curl.php I have some text and a surveymonkey link, which is displayed after the contents of index.php are shown).  Now, what I don't want it to do is show the contents of index.php, at all.  I just want the text and that one link to show up.  How can I do that?

Link to comment
Share on other sites

You want the CURLOPT_RETURNTRANSFER setting.  This will capture the response to your cURL post in a variable and then you can do what you want with it (i.e. output it or not).

 

<?php
// Your current cURL initialization and settings stay as you have them

// Tell cURL you want to capture the response (not render it to the browser)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

// Then assign the cURL response to a variable
$output = curl_exec($ch);

// Then close the cURL connection as usual
curl_close($ch);

// Do whatever you want with the $output variable.
?>

Link to comment
Share on other sites

You want the CURLOPT_RETURNTRANSFER setting.  This will capture the response to your cURL post in a variable and then you can do what you want with it (i.e. output it or not).

 

<?php
// Your current cURL initialization and settings stay as you have them

// Tell cURL you want to capture the response (not render it to the browser)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

// Then assign the cURL response to a variable
$output = curl_exec($ch);

// Then close the cURL connection as usual
curl_close($ch);

// Do whatever you want with the $output variable.
?>

 

Thanks, much appreciated.

 

Ok, I got another problem.  You know how I said that this was working before?  Something I did broke it.  I printed out the $_POST array in my curl.php file and it's getting there, so I have no idea why it decided to not work anymore.  Are there any debug steps that I can take to figure out why it's bombing?

Link to comment
Share on other sites

<?php
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://webstage.intra.lifespan.org/applications/inquiz/index.php?survey=custom-cme-modsedation");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$output = curl_exec($ch);

// get the error, if any.
//echo "<pre>";
//echo (curl_error($ch));

// close cURL resource, and free up system resources
curl_close($ch);

/*echo "<pre>";
print_r($_POST);
die();*/

echo "<p>The survey is complete. Thank you for your time.</p>";

echo "<a href=\"http://www.surveymonkey.com/s/G22Y9TR\">Survey Evaluation</a>";
?>

This is the cURL code.

 

I don't get a specific error, but when the stuff gets passed on to the other index.php, it's not getting transferred over.

Link to comment
Share on other sites

So you can recognise that the output from cURL is from the other index.php but it displays as if there was no POST data submitted?

Ok, maybe I can answer your question with my explanation.  This is how it works, as it should.

+-----------------+      +-----------------+      +-----------------+
|                 |      |                 |      |                 |
|  foo/index.php  |  ->  |  foo/curl.php   |  ->  |    index.php    |
|                 |      |                 |      |                 |
+-----------------+      +-----------------+      +-----------------+

The array of info that gets passed from foo/index.php (that's where a quiz that the user takes is shown), to foo/curl.php (which displays the surveymonkey link and forwards the $_POST from the previous page to the next index.php), to index.php which stores the results in a database.  Now, I've found out that this did do it at one point, but I don't know why it stopped doing this.

 

One weird thing that I did was print the $_POST in foo/curl.php and index.php (the last file in the scheme of things).  What I found was that the $_POST array is not the same in all instances.  Below is the output that I see in curl.php (I commented out curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); to see all of this) after viewing the source in Firefox.  One weird thing is that the $_POST array in foo/curl.php (separated by '=' characters) is longer than the one listed in index.php (this file begins withe the <!DOCTYPE).  Why would this happen?

Array
(
    [survey] => custom-cme-modsedation
    [questionnumber] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
            [10] => 11
            [11] => 12
            [12] => 13
            [13] => 14
            [14] => 15
            [15] => 16
            [16] => 17
            [17] => 18
            [18] => 19
            [19] => 20
            [20] => 21
            [21] => 22
            [22] => 23
            [23] => 24
            [24] => 25
            [25] => 26
            [26] => 27
            [27] => 28
            [28] => 29
            [29] => 30
            [30] => 31
            [31] => 32
            [32] => 33
            [33] => 34
            [34] => 35
            [35] => 36
        )

    [response_1] => 32412312
    [response_2] => Array
        (
            [0] => A
        )

    [response_3] => A
    [response_4] => True
    [response_5] => Array
        (
            [0] => A
        )

    [response_6] => Array
        (
            [0] => A
        )

    [response_7] => A
    [response_8] => A
    [response_9] => A
    [response_10] => Array
        (
            [0] => A
        )

    [response_11] => A
    [response_12] => A
    [response_13] => True
    [response_14] => A
    [response_15] => True
    [response_16] => True
    [response_17] => A
    [response_18] => A
    [response_19] => A
    [response_20] => A
    [response_21] => A
    [response_22] => C
    [response_23] => D
    [response_24] => A
    [response_25] => A
    [response_26] => A
    [response_27] => A
    [response_28] => A
    [response_29] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
        )

    [response_30] => B
    [response_31] => qwq
    [response_33] => 123
    [response_34] => qwq123
    [response_35] => A
)
================================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="/style.css">
<link rel="home" href="http://intra.lifespan.org/">
<link rel="find" href="http://intra.lifespan.org/Search/">
<script language="JavaScript" type="text/javascript">
        <!--
        function init() {
        }
        //-->
        </script>
<style type="text/css">
<!--
.invalid {color: red}
.status {font-size: 12px; font-style: italic; padding: 0; margin-top: 0.5em; margin-bottom: 0.5em}
.question {margin-top: 1em; margin-bottom: 1em}
.useridbox {float: right; font-size: 11px; color: gray; padding-left: 1em; white-space: nowrap}
.userid {font-weight: bold}

/* For answer report */
.choice.correct {font-weight: bold}
.answerChoice.correct {font-weight: bold}
.answerChoice.correctresponse {font-weight: bold;color: red}
.choice.incorrect {color: red}
.rationale {margin: 0.7em 0 0 0}
.answer .value {font-weight: bold}
.choicesBox {margin-left: 1em}
.answerBox {background-color: #EEE; padding: 1em; margin: 1em 1.3em 2em 1.3em}

-->
</style>
<meta name="Formatter" content="ShearerSite 1.5b17">
</head>

<body bgcolor="#FFFFFF" onloadfromcontent onload="init()">
<!-- begin header -->
<div class="intraHeaderStripeButtons"><map
  name="m_HeaderButtons" id="m_HeaderButtons">
  <area shape="rect" coords="0, 1, 23, 17" href="/Phonebook/" title="Phonebook" alt="Phonebook">
  <area shape="rect" coords="30, 1, 83, 17" href="/Search/" title="Search" alt="Search">
  <area shape="rect" coords="90, 0, 167, 17" href="/HomePage/SiteGuide.asp" title="Site Guide" alt="Site Guide" >
  <area shape="rect" coords="170, 0, 215, 17" href="/IS/Help/" title="Help" alt="Help">
</map><img src="/_borders/HeaderGradient.jpg" alt="" width="254" height="18"><img
src="/_borders/HeaderButtons.gif" usemap="#m_HeaderButtons" alt=""
width="215" height="18"></div>

<div class="intraHeaderStripeLogo"><a href="/"><img
src="/_borders/IntraLogoSmall.gif" alt="Intranet Home Page" width="120" height="22"></a>
</div>

<div id="template_placeholder_upper-navigation"> </div>

<!-- end header -->

<div id="template_content">
Array
(
    [survey] => custom-cme-modsedation
    [questionnumber] => Array
    [response_1] => 32412312
    [response_2] => Array
    [response_3] => A
    [response_4] => True
    [response_5] => Array
    [response_6] => Array
    [response_7] => A
    [response_8] => A
    [response_9] => A
    [response_10] => Array
    [response_11] => A
    [response_12] => A
    [response_13] => True
    [response_14] => A
    [response_15] => True
    [response_16] => True
    [response_17] => A
    [response_18] => A
    [response_19] => A
    [response_20] => A
    [response_21] => A
    [response_22] => C
    [response_23] => D
    [response_24] => A
    [response_25] => A
    [response_26] => A
    [response_27] => A
    [response_28] => A
    [response_29] => Array
    [response_30] => B
    [response_31] => qwq
    [response_33] => 123
    [response_34] => qwq123
    [response_35] => A
)
<p>The survey is complete.  Thank you for your time.</p><a href="http://www.surveymonkey.com/s/G22Y9TR">Survey Evaluation</a>

 

It would be easier to view the above in a text-editor and not just in-browser :) .

Link to comment
Share on other sites

Ok, I've had a little bit of insight.  I messed with the form in foo/index.php and this is what I've found:

	<div id="mid">
	<div id="title">CME Mod Sedation Test</div>
        <!-- <form method="post" action="/applications/inquiz/index.php?survey=custom-cme-modsedation" name="questionForm" id="questionForm" style="margin-left: 58px;"> -->
	<form method="post" action="/applications/inquiz/custom/cme/modsedation/curl.php" name="questionForm" id="questionForm" style="margin-left: 58px;">
		<input type="hidden" name="survey" value="custom-cme-modsedation">

		<div class="question">Name <input type="hidden" name="questionnumber[]" value="1"><input type="text" name="response_1" value="" size="40"></div>

 

Look at lines 3 and 4.  Whenever I call the index.php directly (the last to be called in the sequence), it works fine.  But when going through curl.php, not so much.  This seems to be confirmed when looking at the 2 different $_POST from foo/curl.php and index.php (they are structured differently).

 

Why would this change?  Did the array get compacted or something?

Link to comment
Share on other sites

Ok, one insight is that when $_POST is passed from foo/index.php to foo/curl.php (and printed out in foo/curl.php), it looks like this:

Array
(
    [survey] => custom-cme-modsedation
    [questionnumber] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
            [10] => 11
            [11] => 12
            [12] => 13
            [13] => 14
            [14] => 15
            [15] => 16
            [16] => 17
            [17] => 18
            [18] => 19
            [19] => 20
            [20] => 21
            [21] => 22
            [22] => 23
            [23] => 24
            [24] => 25
            [25] => 26
            [26] => 27
            [27] => 28
            [28] => 29
            [29] => 30
            [30] => 31
            [31] => 32
            [32] => 33
            [33] => 34
            [34] => 35
            [35] => 36
        )

    [response_1] => 32412312
    [response_2] => Array
        (
            [0] => A
        )

    [response_3] => A
    [response_4] => True
    [response_5] => Array
        (
            [0] => A
        )

    [response_6] => Array
        (
            [0] => A
        )

    [response_7] => A
    [response_8] => A
    [response_9] => A
    [response_10] => Array
        (
            [0] => A
        )

    [response_11] => A
    [response_12] => A
    [response_13] => True
    [response_14] => A
    [response_15] => True
    [response_16] => True
    [response_17] => A
    [response_18] => A
    [response_19] => A
    [response_20] => A
    [response_21] => A
    [response_22] => C
    [response_23] => D
    [response_24] => A
    [response_25] => A
    [response_26] => A
    [response_27] => A
    [response_28] => A
    [response_29] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
        )

    [response_30] => B
    [response_31] => qwq
    [response_33] => 123
    [response_34] => qwq123
    [response_35] => A
)

But once transferred from foo/curl.php to index.php, it looks like this:

Array
(
    [survey] => custom-cme-modsedation
    [questionnumber] => Array
    [response_1] => 32412312
    [response_2] => Array
    [response_3] => A
    [response_4] => True
    [response_5] => Array
    [response_6] => Array
    [response_7] => A
    [response_8] => A
    [response_9] => A
    [response_10] => Array
    [response_11] => A
    [response_12] => A
    [response_13] => True
    [response_14] => A
    [response_15] => True
    [response_16] => True
    [response_17] => A
    [response_18] => A
    [response_19] => A
    [response_20] => A
    [response_21] => A
    [response_22] => C
    [response_23] => D
    [response_24] => A
    [response_25] => A
    [response_26] => A
    [response_27] => A
    [response_28] => A
    [response_29] => Array
    [response_30] => B
    [response_31] => qwq
    [response_33] => 123
    [response_34] => qwq123
    [response_35] => A
)

It's as if when transferred over, the array was collapsed/flattend/squished.

 

I did some more reading on CURLOPT_POSTFIELDS and this is what I've found:

http://us.php.net/manual/en/function.curl-setopt.php#107621

 

If CURLOPT_POSTFIELDS doesn't work, then how can one pass $_POST around?  index.php _needs_ to have nested sub-arrays.

 

Help!  Now I'm really lost! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.