cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Re: Get the User-Agent string?

From: Tom Worster <fsb_at_thefsb.org>
Date: Thu, 11 Nov 2010 16:44:35 -0500

Maybe example code would help explain what I need.

In this fragment, the value of $curls_ua on line 2 is not set.

$url = 'http://foo.bar/etc/etc';
$my_ua = 'My application (' . $curls_ua . ')';
$h = curl_init();
curl_setopt_array($h, array(
  CURLOPT_URL => $url,
  CURLOPT_FOLLOWLOCATION=> 1,
  CURLOPT_AUTOREFERER => 1,
  CURLOPT_RETURNTRANSFER=> 1,
  CURLOPT_TIMEOUT => 15,
  CURLOPT_USERAGENT => $my_ua
));
$r = curl_exec($h);
curl_close($h);

I want is to set $curls_ua to the well known cURL UA string appropriate
for the platform the code runs on. Thus if the code runs on my iMac then
curls_ua would have the value:

'curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l
zlib/1.2.3'

and the UA string in the HTTP request is:

'My application (curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7
OpenSSL/0.9.8l zlib/1.2.3)'

If the code runs on my FreeBSD box it would be:

'My application (curl/7.21.1 (i386-portbld-freebsd8.1) libcurl/7.21.1
OpenSSL/0.9.8n zlib/1.2.3)

And on the linux box:

'My application (User-Agent: curl/7.15.5 (i686-redhat-linux-gnu)
libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5)'

etc...

There are a lot of servers in the world that search UA strings for the
keywords that characterize well known HTTP clients. So I want to include
the appropriate keywords for cURL in my UA strings so that servers feel
comfortable with what they know (whatever that means: basically I don't
want to send a completely arbitrary, made-up UA string). It's certainly
debatable whether this is a good idea or worth anything but that's a
different question from the one I asked.

On 11/11/10 3:54 PM, "Andrey Kuznetsov" <kandrey89_at_gmail.com> wrote:

>I think you might be confusing curl.
>
>You use curl to specify the url, request type, and other header fields
>like user agent, and etc.
>The webpage that receives the request, suppose its a php script will
>get this data, then if you want the webpage to parse the useragent,
>then access this variable which will be set based on the information
>sent to it by the curl, $_SERVER['HTTP_USER_AGENT'].
>
>I believe you can set ANY useragent field you want, there is no strict
>format, especially since it seems like you want to use it for
>yourself, so there probably won't be an outside incompatibilities.
>
>Basically, you should be able to do this:
>curl_setopt($ch, CURLOPT_USERAGENT, 'Hello World, This is My NEW
>USERAGENT string, it is custom made');
>
>then when the page receives the request from curl, this variable will be
>=>
>$_SERVER['HTTP_USER_AGENT'] == 'Hello World, This is My NEW USERAGENT
>string, it is custom made'
>
>On Thu, Nov 11, 2010 at 12:32 PM, Tom Worster <fsb_at_thefsb.org> wrote:
>> Curl does things like this:
>>
>> GET / HTTP/1.1
>> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7
>> OpenSSL/0.9.8l zlib/1.2.3
>> Host: 127.0.0.1:12345
>> Accept: */*
>>
>>
>> That's a swell User-Agent value. Very informative.
>>
>> In my application, I want to send an informative User-Agent such as this
>> but modified to identify also my application. Hence I want to find the
>> proper UA string for the php curl instance on the server so that I can
>> manipulate it.
>>
>> So can I read such a string value in a PHP script?
>>
>> On other words: We are given a UA setter curl_setopt(,
>> CURLOPT_USERAGENT,). Is there a corresponding UA getter?
>>
>> Tom
>>
>>
>> On 11/11/10 3:02 PM, "Andrey Kuznetsov" <kandrey89_at_gmail.com> wrote:
>>
>>>You are unclear about what you mean, sending HTTP GET to oneself, what
>>>does that mean?
>>>
>>>If you want to send the useragent of the browser that is running the
>>>script the use $_SERVER['HTTP_USER_AGENT'] value.
>>>
>>>On Thu, Nov 11, 2010 at 11:54 AM, Tom Worster <fsb_at_thefsb.org> wrote:
>>>> We can set the User-Agent string that cURL will use with
>>>>
>>>> curl_setopt($handle, CURLOPT_USERAGENT, 'Something');
>>>>
>>>> But is there a way to get this this string value without sending an
>>>>HTTP
>>>> GET to oneself?
>>>>
>>>> Tom
>>>>
>>>>
>>>> _______________________________________________
>>>> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
>>>>
>>>
>>>
>>>
>>>--
>>>Andrey
>>>_______________________________________________
>>>http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
>>
>>
>> _______________________________________________
>> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
>>
>
>
>
>--
>Andrey
>_______________________________________________
>http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2010-11-11