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

?>

Recommended Answers

All 6 Replies

Hi,

read the notice and the warning:

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

The notice tells you that RETURNTRANSFER is not defined. The PHP engine in this case makes an assumption: you probably meant to use it as a string, so it dress the constant with quotes and serve it to the code.

The warning is just curl_setopt() complaining because, by consequence of the PHP engine assumption, received a string, when it was expecting an integer. So, have you checked if, among curl constants, there is something like RETURNTRANSFER?

AndrisP,

Another programmer elsewhere suggested the same as you and that error is gone now 5 mins ago when I fixed it.

Thanks!

img open "<" tag is missing in line 49 and required attribute "alt" is missing also - alt text is very necessary for people with sight problems. Alt text always should be say something about picture for using screen reader.

@UI

in addition to Andris, the first 16 lines are useless, because it makes a request to google and it does not use it. Line 21 ($url) is not used by the following code, so curl sends a request to the homepage not to the search. Even by changing that, to run the query and set an additional fake user agent, it will hit against a robot check:

<!--
    To discuss automated access to Amazon data please contact api-services-support@amazon.com.
    For information about migrating to our APIs refer to our Marketplace APIs at https://developer.amazonservices.com/ref=rm_c_sv, or our Product Advertising API at https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html/ref=rm_c_ac for advertising use cases.
-->

or status code 503 from CloudFront. Why? Try running the link you want to access through the command line curl:

curl -s -D - https://www.amazon.com/s/field-keywords=movies+2017 -o /dev/null

You get:

HTTP/1.1 301 Moved Permanently
...
Location: https://www.amazon.com/movies-2017/s?ie=UTF8&page=1&rh=i%3Aaps%2Ck%3Amovies%2B2017

which means you have to add a curl option to follow redirects. Would now work? Yes, but it probably won't return results because this part of the pattern ._AC_US_160_.jpg, in the preg match expression, is not pointing to what is currently returned by Amazon results page. So, you can:

  • open the source page and verify what is in use and hard code the change
  • or modify the pattern to be more flexible to code changes

in your current code it returns what is in the scr and in srcset attributes, so you can get an extended list of links for each entry and you could apply another kind of filtering (the smaller for each link) or point the pattern to match only the src attribute.

Also, this line had the double quotes missing after the .jpg which not a single programmer out of the 3 forums spotted when they told me the same thing as you:
I only spotted it after going through the vid again at the end for half a min.

To be honest this sentence irritates me, as you see there are many things to remark about the script, you asked about a specific error and you got two answers to solve the issue. If you spot other errors, read the message and try to fix it by yourself or ask if it is difficult. It is not our duty to make sure your code is fine, it's yours.

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.