noon Posted December 11, 2007 Share Posted December 11, 2007 I have two test pages set up on my local apache server. Here is the code for each curl.php <?php // Initialize the connection $ch = curl_init('http://localhost:81/phun/receive.php'); // FAIL on error! curl_setopt ($ch, CURLOPT_FAILONERROR, 1); // Allow redirects curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Assign returned data to var curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Set timeout (in seconds) curl_setopt($ch, CURLOPT_TIMEOUT, 15); // Let's POST the data curl_setopt ($ch, CURLOPT_POST, 1); // Set that POST curl_setopt ($ch, CURLOPT_POSTFIELDS, "post_test=true&name=ryan"); // Execute!, snatch the output $output = curl_exec($ch); // Grab any curl info before we terminate connection $info = curl_getinfo($ch); // Close the connection curl_close($ch); // Let's have a look-sey echo '<h1>cURL Results:</h2><pre>' . htmlspecialchars($output) . '</pre>'; echo '<h1>curl_getinfo() Results:</h2><pre>' . print_r($info) . '</pre>'; ?> receive.php <?php echo '<h1>Hello!</h1>'; if (!empty($_POST['post_test']) && !empty($_POST['name'])) { echo "Success!<br />post_test = $_POST['post_test']<br />name = $_POST['name']"; } else { echo 'You are here with $_POST vars unset!<br />'; echo '$_POST = <br />'; //echo var_dump($_POST); } ?> What I am receiving after I run curl.php <h1>cURL Results:</h2><pre></pre>Array ( [url] => http://localhost:81/phun/receive.php [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [upload_content_length] => 0 [starttransfer_time] => 0 [redirect_time] => 0 ) <h1>curl_getinfo() Results:</h2><pre>1</pre> I don't understand how that information is getting printed between the h1 tags. It's not $output because its AFTER the </pre> tag. It's not $info because its before the next <h1> tag. So confused... Thanks Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted December 11, 2007 Share Posted December 11, 2007 check out this cURL tutorial http://www.phpit.net/article/using-curl-php/ Quote Link to comment Share on other sites More sharing options...
noon Posted December 11, 2007 Author Share Posted December 11, 2007 Thanks for the tutorial, I already have four. I want to know why my script is doing that. I have my curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); set appropriately to capture the data to var and not print. And that really doesn't tell me anything I don't already know. Quote Link to comment Share on other sites More sharing options...
rlindauer Posted December 11, 2007 Share Posted December 11, 2007 echo '<h1>cURL Results:</h2>pre>' . htmlspecialchars($output) . '</pre>'; echo '<h1>curl_getinfo() Results:</h2><pre>' . print_r($info) . '</pre>'; You are closing the H1 tags with a closing H2. Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted December 11, 2007 Share Posted December 11, 2007 I think the reason why it wasn't working is that your post vars might not have been submitted correctly. try <?php // Initialize the connection $ch = curl_init(); $url = "http://localhost:81/phun/receive.php"; curl_setopt($ch, CURLOPT_URL,$url); // set url to post to // FAIL on error! curl_setopt ($ch, CURLOPT_FAILONERROR, 1); // You don't need redirects for this, so I removed it // Assign returned data to var curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set timeout (in seconds) curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Let's POST the data curl_setopt($ch, CURLOPT_POST, 1); // Set that POST //OLD .. curl_setopt ($ch, CURLOPT_POSTFIELDS, "post_test=true&name=ryan"); curl_setopt($ch, CURLOPT_POSTFIELDS, array('post_test'=>"true", 'name'=>"Ryan")); // add POST fields // Execute!, snatch the output $output = curl_exec($ch); // Grab any curl info before we terminate connection $info = curl_getinfo($ch); // Close the connection curl_close($ch); // Let's have a look-sey echo '<h1>cURL Results:</h2><pre>' . htmlspecialchars($output) . '</pre>'; echo '<h1>curl_getinfo() Results:</h2><pre>' . print_r($info) . '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
noon Posted December 11, 2007 Author Share Posted December 11, 2007 Same result. Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted December 11, 2007 Share Posted December 11, 2007 for the receive.php try <?php echo '<h1>Hello!</h1>'; if (isset($_POST['post_test']) && isset($_POST['name'])) { echo "Success!<br />post_test = {$_POST['post_test']}<br />name = {$_POST['name']}"; } else { echo 'You are here with $_POST vars unset!<br />'; echo '$_POST = <br />'; //echo var_dump($_POST); } ?> Quote Link to comment Share on other sites More sharing options...
sureshp Posted December 11, 2007 Share Posted December 11, 2007 Try the below one. I hope that it will work. Because, I had used the same sample before a year ago and it worked for me. <?php $ch = curl_init(); $res= curl_setopt ($ch, CURLOPT_URL,"https://yoururl/cgi"); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "Idc=si&"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $xyz = curl_exec ($ch); curl_close ($ch); echo $xyz; ?> Quote Link to comment Share on other sites More sharing options...
noon Posted December 11, 2007 Author Share Posted December 11, 2007 Well at least now I get a completely blank output. It must be timing out. It must be something with my machine. I'll try this when I get home. *grumble, stupid work computer...* Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted December 11, 2007 Share Posted December 11, 2007 I made this work, but if it doesn't work for you, then you have problems not related to this code: cURL.php <?php // create a new curl resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://localhost:81/phun/receive.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Let's POST the data curl_setopt($ch, CURLOPT_POST, 1); // Set that POST curl_setopt($ch, CURLOPT_POSTFIELDS, array('post_test'=>"true", 'name'=>"Ryan")); // add POST fields // grab URL, and return output $output = curl_exec($ch); // Print output echo '<h1>cURL Results:</h1><p>' . $output . '</p>'; echo '<h1>curl_getinfo() Results:</h1>'; echo '<pre>'; print_r (curl_getinfo($ch)); echo '</pre>'; ?> receive.php <?php if(isset($_POST['post_test']) && isset($_POST['name'])){ echo "post_test == {$_POST['post_test']}"; echo "<br>"; echo "name == {$_POST['name']}"; }else{ echo "there was a problem with your cURL post"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.