0

I am trying to pass a custom browser user_agent with cURL in PHP.

Error I am getting:

Undefined variable $url in index-curl.php on line XX

$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);   
    curl_setopt($ch, CURLOPT_VERBOSE, true);    

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$homepage = file_get_contents_curl("https://www.example.com");
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mehul Kumar
  • 461
  • 8
  • what do you mean by custom UA? An UA is a browser feature, it would be a security risk if a website/application could replace it and potentially destroy a browser. – tacoshy Feb 01 '23 at 07:54
  • 1
    `User-Agent` is just another header. Look up how to set headers with PHP cURL – Phil Feb 01 '23 at 07:59
  • 1
    @tacoshy What are you on about? – DarkBee Feb 01 '23 at 08:00
  • @DarkBee how can i fix the error? – Mehul Kumar Feb 01 '23 at 08:06
  • If you're still getting an error about `$url`, then the code in your question is not an accurate representation of your actual code. That being said, if the error is actually about `$agent, it's because PHP variable scope does not nest into functions – Phil Feb 01 '23 at 08:06
  • @Phil `function file_get_contents_curl($url, $agent)` in fuction but still same error. – Mehul Kumar Feb 01 '23 at 08:09
  • 1
    You've misunderstood. Move the `$agent = ...` line **inside** your function – Phil Feb 01 '23 at 08:11
  • @Phil yes it working. So, any value defined outside the function will not work inside the function? – Mehul Kumar Feb 01 '23 at 08:15

1 Answers1

1

Is this code working for you?

<?php
function file_get_contents_curl($curl_url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $curl_url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$homepage = file_get_contents_curl("https://example.com");

echo $homepage;
?>
David
  • 2,898
  • 3
  • 21
  • 57
  • sorry, that a typo mistake. but my error still not fixed – Mehul Kumar Feb 01 '23 at 08:05
  • @David yes, this is fine. I see you have declared `user_agent` directly inside function. But what is wrong on my code. it same like declaring directly or declaring using user-defined value `$something` ?. – Mehul Kumar Feb 01 '23 at 08:12
  • 1
    @MehulKumar A function cannot access a normal variable from the outside. – David Feb 01 '23 at 12:43
  • 1
    @MehulKumar If needed you could pass the user agent as a parameter: Change as follows: `function file_get_contents_curl($curl_url, $user_agent) {` and `$homepage = file_get_contents_curl("https://example.com", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322");` and `curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);` – David Feb 01 '23 at 12:49