Link to home
Create AccountLog in
Avatar of sabecs
sabecs

asked on

Problem passing a variable to a function?

Hi,
I am having problems getting this to work, why can't I pass a variable to my function get_auspost_api?

Thanks in advance for your help.


<?php

//THIS WORKS

function get_auspost_api($url){
		 $crl = curl_init();
		 $timeout = 5;
		 curl_setopt ($crl, CURLOPT_HTTPHEADER, array('AUTH-KEY:yrtyyukgvGg66e7WBLtA77AtWUQripv'));
		 curl_setopt ($crl, CURLOPT_SSL_VERIFYPEER, false);
		 curl_setopt ($crl, CURLOPT_URL, $url);
		 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
		 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
		 $ret = curl_exec($crl);
		 curl_close($crl);
		 
		 return $ret;
}



//THIS DOS NOT WORK		

$aus_key = 'AUTH-KEY:yrtyyukgvGg66e7WBLtA77AtWUQripv';

function get_auspost_api($url){
		 
		 global $aus_key ;

		 $crl = curl_init();
		 $timeout = 5;
		 curl_setopt ($crl, CURLOPT_HTTPHEADER, array($aus_key));
		 curl_setopt ($crl, CURLOPT_SSL_VERIFYPEER, false);
		 curl_setopt ($crl, CURLOPT_URL, $url);
		 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
		 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
		 $ret = curl_exec($crl);
		 curl_close($crl);
		 
		 return $ret;
}

?>

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Do you get an error or does it simply not do what it is supposed to?

If you do this

$arr = array($aus_key)

At the start of the function and then

curl_setopt ($crl, CURLOPT_HTTPHEADER, $arr)

Does that work?
THIS DOS NOT WORK
C'mon -- give us a hint... What error message do you get?  What output do you get?

If you want to see how cURL works, this code sample shows a successful cURL setup.

<?php // RAY_curl_get_example.php
error_reporting(E_ALL);


// DEMONSTRATE THE BASICS OF CURL
// SOMETHING LIKE RAY_curl_get_example.php?url=http://twitter.com


// YOU COULD HAVE SOMETHING LIKE THIS
$url = isset($_GET["url"]) ? $_GET["url"] : 'http://twitter.com';

// BUT SINCE IT IS ON MY SERVER, I HAVE HARD-CODED THIS
$url = 'https://twitter.com/RayPaseur';

// TRY THE REMOTE WEB SERVICE
$htm = my_curl($url);

// SHOW THE WORK PRODUCT OR BARK OUT ERROR MESSAGES
echo "<pre>";
echo PHP_EOL . '<strong>' . $url . '</strong>';
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  ); // ANCIENT HISTORY
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Agree with darren-w- 100%.  The "global" designation is a time-bomb.  Data encapsulation is the professional way of doing things, and the last thing you want is a global variable!
Alternatively use a define if this is a config type setting
Avatar of sabecs
sabecs

ASKER

Thanks to everyone for your comments..