Jump to content

cURL Help


noon

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
  
?>

Link to comment
Share on other sites

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);
  }
?>

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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";
}
?>


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.