Error: Use of undefined constant RETURNTRANSFER

Hi,

Does anyone have a clue why I get the following error in my further following code ?

Notice: Use of undefined constant RETURNTRANSFER - assumed ‘RETURNTRANSFER’ in C:\xampp\htdocs\test\curl.php on line 27

Warning: curl_setopt() expects parameter 2 to be integer, string given in C:\xampp\htdocs\test\curl.php on line 27

You are welcome to rectify the code where I’m goign wrong and then show me how it should have been.
I actually, copied this from a youtube tutorial and they don;t get any errors but I do! Absurd! I copied it while I was learning the basics of cURL.


<?php 

// Youtube Channel: Clever Techie; Video: PHP cURL Tutorial - Learn PHP Programming.

// 1). Create cURL resource.
$curl = curl_init();

// 2). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'http://www.google.com');

// 3). Run cURL (execute http request).
curl_exec($curl);

// 4). Close cURL resource.
curl_close($curl);


// 1). $curl is going to be data type curl resource.
$curl = curl_init();

$search_string = "movies 2017";
$url = "https://www.amazon.com/s/field-keywords=$search_string";

// 2). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.amazon.com');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, RETURNTRANSFER, true);

// 3). Run cURL (execute http request).
$result = curl_exec($curl);
preg_match_all("!https://images-na.ssl-images-amazon.com/images/I/[^\s]*?._AC_US_160_.jpg!, $result, $matches");

//Use either one of the following 4 codes. (Use one by one in order to test and learn).

//code 1:
print_r($matches);

//code 2:
$images = array_unique($matches[0]);
print_r($images);

//code 3:
$images = array_values(array_unique($matches[0]));
print_r($images);

//code 4:
$images = array_values(array_unique($matches[0]));
for ($i = 0; $i < count ($images); $i++) {
    echo "<div style='float:left; margin: 10 0 0 0; '>";
    echo "img src='$images[$i]'><br />";
	echo "</div>";
}

// 4). Close cURL resource.
curl_close($curl);

?>

because it’s CURLOPT_RETURNTRANSFER

see http://php.net/manual/en/curl.constants.php for all the available constants.

2 Likes

Also, this line had the double quotes missing after the .jpg and wrongfully placed after the $matches which not a single programmer out of the 3 forums spotted when they told me the same thing as you:

preg_match_all(“!https://images-na.ssl-images-amazon.com/images/I/[^\s]*?.ACUS160_.jpg!”, $result, $matches);

I only spotted it after going through the vid again at the end for half a min.

https://www.youtube.com/watch?v=1X2-UEUqrd8&t=21s

Thanks!


<?php 

// Youtube Channel: Clever Techie; Video: PHP cURL Tutorial - Learn PHP Programming.

// 1). Create cURL resource.
$curl = curl_init();

// 2). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'http://www.google.com');

// 3). Run cURL (execute http request).
curl_exec($curl);

// 4). Close cURL resource.
curl_close($curl);


// 1). $curl is going to be data type curl resource.
$curl = curl_init();

$search_string = "movies 2017";
$url = "https://www.amazon.com/s/field-keywords=$search_string";

// 2). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.amazon.com');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// 3). Run cURL (execute http request).
$result = curl_exec($curl);
preg_match_all("!https://images-na.ssl-images-amazon.com/images/I/[^\s]*?._AC_US160_.jpg!", $result, $matches);

//Use either one of the following 4 codes. (Use one by one in order to test and learn).

//code 1:
//print_r($matches);

//code 2:
//$images = array_unique($matches[0]);
//print_r($images);

//code 3:
/*$images = array_values(array_unique($matches[0]));
print_r($images);
*/

//code 4:
$images = array_values(array_unique($matches[0]));
for ($i = 0; $i < count ($images); $i++) {
    echo "<div style='float:left; margin: 10 0 0 0; '>";
    echo "img src='$images[$i]'><br />";
	echo "</div>";
}


// 4). Close cURL resource.
curl_close($curl);

?>

To be fair, they were probably concentrating on solving the problem you asked about. Not everyone will then go on to look for other issues, especially given how many times people post code that isn’t actually the code they’re using.

2 Likes

Actually, you are right.
When I look into others’ codes (eg. miniProxy) it does my head in and so I only drop my eyes on my own code.
As for youtube tutorials. That’s a different issue because on those they write code bit by bit and so by the time they go at the end, I can still figure-out what the code is for. Otherwise, I would have gone mad.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.