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
https://forums.phpfreaks.com/topic/81202-curl-help/
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
https://forums.phpfreaks.com/topic/81202-curl-help/#findComment-412122
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
https://forums.phpfreaks.com/topic/81202-curl-help/#findComment-412172
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
https://forums.phpfreaks.com/topic/81202-curl-help/#findComment-412180
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
https://forums.phpfreaks.com/topic/81202-curl-help/#findComment-412196
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.