hi all,

ive been using curl with a few different 3rd parties, but im trying to get me head around what i should be returning.

im using this curl function

<?php
function request($url, $postdata) {
$curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($curl, CURLOPT_POST, 1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
   curl_setopt($curl, CURLOPT_HEADER, 0);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec($curl);
   curl_close($curl);
   return $result; 
}

$url = "mydomain.com/curl.php?";
$postdata = "id=1";

?>

my question is, what do i have in the curl.php file to get a response to show in the screen?

Regards

Paul

Forgive me for being obvious:
If you're not calling the function, that might be the problem.

<?php
function request($url, $postdata) {
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($curl, CURLOPT_POST, 1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
   curl_setopt($curl, CURLOPT_HEADER, 0);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec($curl);
   curl_close($curl);
   return($result); 
}

$url = "mydomain.com/curl.php?";
$postdata = "id=1";
echo request($url, $postdata);
?>

But I'm assuming you are calling the function since most of your 400+ posts are in this forum. If you are, my guess is that the website you are querying with cURL has blocked your server from requesting its pages. Google does this when bots & scripts overuse its search page. Other popular sites might employ similar techniques.

hi thanks for your response.

is line 17 not the call for this?

my page names curl.php is simple a get variable that echo's the id parameter, all within the same domain.

Is there a setting i need to use in htaccess to allow for this?

Line 17 in my code is the call to your function. In your code, that line is blank! :P

my page names curl.php is simple a get variable that echo's the id parameter, all within the same domain.

Umm, what? Your curl.php page takes a GET variable that echoes the id? Does it already, or is that what you want. If you haven't created curl.php yet, it seems simple enough!

curl.php

<?php
echo $_GET['id']; 
echo $_POST['id']; //You use post in cURL, so I included it for good measure
?>

You know: If curl.php is on the same domain, you can just include or require the file. It will have the same effect.

<?php
require('curl.php?id=1');
?>

Best,
PhpMyCoder

hi,

i dont think ive been very clear.

At present i use curl to integrate with a number of 3rd party systems, but all i do is create the url and post data. I then deal with the results ($results) in xml format.

Im busy trying to get a new guy in the office to understand curl, so the code ive included is my curl function. the curl.php?id=1 exists and works if you go to that URL in its raw format.

What i need to know is, when i run that curl function (both the index.php and the curl.php files do exist), WHAT exactly to i need in the curl.php file that will pass a result back that i can get to display to the screen?

lol, yes it was in, forgot to paste it.

Got it! I mentioned it above, but I've included it again in this post if you need it.

curl.php:

<?php
echo 'Hello cURL! ID: '.$_POST['id'];
?>

Might I suggest a more robust cURL function:

function simpleCURL($url, $get = NULL, $post = NULL, $opts = array()) {
    //Init cURL, returning false with any errors
    if(!($c = @curl_init())) {
        return(false);
    }
    
    //Append any get vars
    if(is_array($get)) {
        $get = array2params($get);
    }
    if(isset($get)) {
        $url .= '?'.$get;
    }

    //Set the cURL URL
    curl_setopt($c, CURLOPT_URL, $url);

    //Set any post vars
    if(is_array($post)) {
        $post = array2params($post); //Turn into a string to prevent multipart/form-data
    }
    if(isset($post)) {
        curl_setopt($c, CURLOPT_POST, true);
        culr_setopt($c, CURLOPT_POSTFIELDS, $post);
    }

    //Set required opts
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_HEADER, false);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

    //Set additional opts
    curl_setopt_array($c, $opts);

    //Execute cURL query, close connection, and return
    $result = @curl_exec($c);
    curl_close($c);
    if(curl_error($c) !== '') {
        return(false);
    }
    return($result);
}

function array2params($array) {
    $params = array();
    foreach($array as $param => $value) {
         $params[] = $param.'='.urlencode($value);
    }
    return(implode('&', $params));
}

if(!function_exists('curl_setopt_array')) {
    function curl_setopt_array(&$c, $options) {
        foreach($options as $option => value) {
            if(!curl_setopt($c, $option, $value)) {
                return(false);
            }
        }
        return(true);
    }
}


//It can be called like so:
simpleCURL('http://www.mysite.com/curl.php', 'id=1');
//Or...
simpleCURL('http://www.mysite.com/curl.php', array('id' => 1));

Does this answer your question?

can you advise on error im getting from dreamweaver on line 52 and 54 please.

It certainly looks meatier than my attempt.

thanks for your help

My mistake, I forgot a $ before the value variable in the definition of curl_setopt_array() . Here's the redo:

<?php
function simpleCURL($url, $get = NULL, $post = NULL, $opts = array()) {
    //Init cURL, returning false with any errors
    if(!($c = @curl_init())) {
        return(false);
    }
    
    //Append any get vars
    if(is_array($get)) {
        $get = array2params($get);
    }
    if(isset($get)) {
        $url .= '?'.$get;
    }

    //Set the cURL URL
    curl_setopt($c, CURLOPT_URL, $url);

    //Set any post vars
    if(is_array($post)) {
        $post = array2params($post); //Turn into a string to prevent multipart/form-data
    }
    if(isset($post)) {
        curl_setopt($c, CURLOPT_POST, true);
        culr_setopt($c, CURLOPT_POSTFIELDS, $post);
    }

    //Set required opts
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_HEADER, false);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

    //Set additional opts
    curl_setopt_array($c, $opts);

    //Execute cURL query, close connection, and return
    $result = @curl_exec($c);
    curl_close($c);
    if(curl_error($c) !== '') {
        return(false);
    }
    return($result);
}

function array2params($array) {
    $params = array();
    foreach($array as $param => $value) {
         $params[] = $param.'='.urlencode($value);
    }
    return(implode('&', $params));
}

if(!function_exists('curl_setopt_array')) {
    function curl_setopt_array(&$c, $options) {
        foreach($options as $option => $value) {
            if(!curl_setopt($c, $option, $value)) {
                return(false);
            }
        }
        return(true);
    }
}


//It can be called like so:
echo simpleCURL('http://www.mysite.com/curl.php', 'id=1');
//Or...
echo simpleCURL('http://www.mysite.com/curl.php', array('id' => 1));
?>

As for line #52, I ran this code on my own server and it complied and ran fine. Are you sure that Dreamweaver isn't just making a mistake? What error is it giving you?
Also, since we've digressed, I forgot to ask: Did I answer your curl.php question?

i have to be missing something here. the curl file includes the following but im getting nothing to the screen.

<?php
echo $_GET['id']; 
echo $_POST['id']; //You use post in cURL, so I included it for good measure
?>

Is this what you're doing:
testfile.php:

<?php
//Define either your function or my function here

echo simpleCURL('url.com/curl.php', 'id=1');
?>

curl.php:

<?php
echo $_REQUEST['id'];
?>

It would really help if I could see the exact code you are using in both the curl.php files and your test file! If you could provide the source for both of these, the problem could be fixed quicker!

Best,
PhpMyCoder

index.php

<?php
function simpleCURL($url, $get = NULL, $post = NULL, $opts = array()) {
    //Init cURL, returning false with any errors
    if(!($c = @curl_init())) {
        return(false);
    }
    
    //Append any get vars
    if(is_array($get)) {
        $get = array2params($get);
    }
    if(isset($get)) {
        $url .= '?'.$get;
    }

    //Set the cURL URL
    curl_setopt($c, CURLOPT_URL, $url);

    //Set any post vars
    if(is_array($post)) {
        $post = array2params($post); //Turn into a string to prevent multipart/form-data
    }
    if(isset($post)) {
        curl_setopt($c, CURLOPT_POST, true);
        culr_setopt($c, CURLOPT_POSTFIELDS, $post);
    }

    //Set required opts
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_HEADER, false);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

    //Set additional opts
    curl_setopt_array($c, $opts);

    //Execute cURL query, close connection, and return
    $result = @curl_exec($c);
    curl_close($c);
    if(curl_error($c) !== '') {
        return(false);
    }
    return($result);
}

function array2params($array) {
    $params = array();
    foreach($array as $param => $value) {
         $params[] = $param.'='.urlencode($value);
    }
    return(implode('&', $params));
}

if(!function_exists('curl_setopt_array')) {
    function curl_setopt_array(&$c, $options) {
        foreach($options as $option => $value) {
            if(!curl_setopt($c, $option, $value)) {
                return(false);
            }
        }
        return(true);
    }
}


//It can be called like so:
echo simpleCURL('http://www.white-it.com/curl/curl.php', 'id=1');
//Or...
echo simpleCURL('http://www.white-it.com/curl/curl.php', array('id' => 1));
?>

curl.php

<?php
echo $_GET['id']; 
echo $_POST['id']; 
?>

very much appreciated. i hate it when ive used something for ages, then a new person asks me how it works.

hi

got this error


Warning: curl_error(): 1 is not a valid cURL handle resource in /home/sites/white-it.com/public_html/curl/index2.php on line 39


line 39 has stars at the end of it. i commented that bit out, and then back to nothing. errors are set to show all.

$result = @curl_exec($c);
    curl_close($c);
    if(curl_error($c) !== '') { *********** line 39
        return(false);
    }
    return($result);
}

i thought it might have been either the ! (leaving ==) or = (leaving !=)

How stupid of me! In a effort to save resources I closed the cURL connection before I checked for errors. The code should read:

$result = @curl_exec($c);
    if(curl_error($c) !== '') {
        curl_close($c);
        return(false);
    }
    curl_close($c);
    return($result);
}

It also might help at the bottom, to remove one of the demos. Otherwise, you'll get a double result.
Sorry for the confusion!

thank you.

im getting nothing to screen, so i can only assume that the curl.php file is missing something.

no matter what i put in the curl.php file, a simple echo "hello", anything at all, i get nothing at all.

im in pain

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.