{"id":2451,"date":"2020-09-25T11:46:08","date_gmt":"2020-09-25T11:46:08","guid":{"rendered":"https:\/\/support.itscope.hostpress.me\/?post_type=kb&#038;p=2451"},"modified":"2022-09-20T15:34:56","modified_gmt":"2022-09-20T13:34:56","slug":"mandatory-user-agent-in-api-requests","status":"publish","type":"kb","link":"https:\/\/guide.itscope.com\/en\/kb\/mandatory-user-agent-in-api-requests\/","title":{"rendered":"Mandatory user agent in API requests"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"1\">User agent<\/h2>\n\n\n\n<p>What is a user agent? See&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/User_agent\">https:\/\/en.wikipedia.org\/wiki\/User_agent<\/a>&nbsp;for a detailed description.<\/p>\n\n\n\n<p>The ITscope Loadbalancer that accepts the API requests has some protection mechanisms for DoS, Flooding and search engine queries. To prevent you from falling victim to these protection mechanisms, you should set user agents when retrieving API requests. The examples listed below should help you to implement your scripts correctly.<\/p>\n\n\n\n<p>Meanwhile, certain user agents are filtered against DoS and Flooding, e.g. <strong>Snoopy<\/strong>, <strong>Smarty<\/strong> but also not set (<strong>zero<\/strong>) user agents.<\/p>\n\n\n\n<p>Our servers also filter certain search engine user agents, e.g. <em>bot spider search seek retriever checker agent crawler gonzo yandex snoopy slurp Indy<\/em>.<\/p>\n\n\n\n<p><strong>ERPs and other applications<\/strong><\/p>\n\n\n\n<p>For applications that always use a fixed user agent, it is sufficient for the user agent to consist of a combination of the company name (name of the company that developed the application), the name of the application and the version of the application.<\/p>\n\n\n\n<p>e.g. Developing company &#8211; Application name &#8211; Application version<\/p>\n\n\n\n<p><strong>Shops and search engines<\/strong><\/p>\n\n\n\n<p>Shops should not send a fixed user agent to the ITscope API. Instead, the user agent is the browser with which the customer navigates in the shop.<\/p>\n\n\n\n<p>For this reason it is very important that the user agent of the requesting customer is forwarded correctly to the ITscope API,&nbsp;in the event that ITscope API calls are used in a shop. This allows our Loadbalancer to recognise that the request comes from a normal user browser and not from a search engine.<\/p>\n\n\n\n<p><strong>Effects<\/strong><\/p>\n\n\n\n<p>If a user agent is identified as a search engine, then the ITscope API Request will be delivered with a varying delay (10-20 seconds). This way, the Loadbalancer prevents the real customer from having unnecessary loading times when using the ITscope services.<\/p>\n\n\n\n<p>Flooding, DoS or no (zero) user agents can be blocked very quickly, so a user agent should always be set for your own security.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2\"><br>Examples of PHP queries<\/h2>\n\n\n\n<p>This section contains instructions regarding PHP scripts for transferring user agents.<br><br>Currently, many web services are delivered to customers with incorrect or no user agent. We therefore ask you to reconfigure your queries according to the instructions below.<\/p>\n\n\n\n<p><strong>Snoopy<\/strong><\/p>\n\n\n\n<p>Changes in the&nbsp;<em>Snoopy.class.php<\/em> file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  \/\/ UserAgent Code Start <br>  \/\/ var $ agent = \"Snoopy v1.2.4\"; <br>  Var $ agent = $ _ SERVER ['HTTP_USER_AGENT']; <br>  \/\/ UserAgent Code End <\/pre>\n\n\n\n<p><strong>Smarty Template Engine<\/strong><br>Changes in the following file:&nbsp;<em>smarty\/libs\/plugins\/function.fetch.php-&gt;s<\/em><em>marty_function_fetch<\/em><br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  \/\/ UserAgent Code Start <br>  \/\/ $ agent = \"Smarty Template Engine\". $ Smarty -&gt; _ version; <br>  $ Agent = $ _SERVER ['HTTP_USER_AGENT']; <br>  \/\/ UserAgent Code End <\/pre>\n\n\n\n<p><strong>Curl<\/strong><br>Expand the curl_setOpt statement<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  \/\/ UserAgent Code Start <br>  Curl_setopt ($ ch, CURLOPT_USERAGENT, $ _SERVER ['HTTP_USER_AGENT']); <br>  \/\/ UserAgent Code End <br><\/pre>\n\n\n\n<p><strong>File_get_contents<\/strong><\/p>\n\n\n\n<p>The function call should look like this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  \/\/ UserAgent Code Start <br>  $ Opts = array ( <br>  'Http' =&gt; array ( <br>  'Header' =&gt; \"User Agent:\".  $ _SERVER ['HTTP_USER_AGENT'].  \"\\ R \\ n\" <br>  ) <br>  ); <br>  $ Context = stream_context_create ($ opts); <br>  $ TheirVariable = file_get_contents ($ url, false, $ context); <br>  \/\/ UserAgent Code End <\/pre>\n\n\n\n<p><strong>Fsockopen<\/strong><\/p>\n\n\n\n<p>Example with user agent<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  $ Fp = @fsockopen ($ url, 80, $ errno, $ errstr, $ timeout); <br>  $ Out = \"GET \/ HTTP \/ 1.0 \\ r \\ n\"; <br>  $ Out. = \"Host: www.example.com \\ r \\ n\"; <br>  \/\/ UserAgent Code Start <br>  $ Out. = 'User Agent:'. $ _ SERVER ['HTTP_USER_AGENT'].  \"\\ R \\ n\"; <br>  \/\/ UserAgent Code End <\/pre>\n\n\n\n<p><strong>Fopen<\/strong><\/p>\n\n\n\n<p>Example with user agent<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  \/\/ UserAgent Code Start <br>  Ini_set ('user_agent', $ _ SERVER ['HTTP_USER_AGENT']); <br>  \/\/ UserAgent Code End <br>  $ Result = @fopen ($ url, \"r\"); <\/pre>\n\n\n\n<p><strong>Varien_Http_Client (Magento)<\/strong><br>Example for the following file: magento\\lib\\Varien\\Http\\client.php<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  Class Varien_Http_Client extends Zend_Http_Client <br>  { <br>  Public function __construct ($ uri = null, $ config = null) <br>  { <br>  \/\/ $ this-&gt; config ['useragent'] = 'Varien_Http_Client'; <br>  $ This-&gt; config ['useragent'] = $ _SERVER ['HTTP_USER_AGENT']; <br>  Parent :: __ construct ($ uri, $ config); <br>  } <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3\">Examples C #<\/h2>\n\n\n\n<p><strong>HttpWebRequest<\/strong><\/p>\n\n\n\n<p>Set the user agent variable in the HttpWebRequest&nbsp;to the name of your company and application<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  HttpWebRequest myHttpWebRequest = HttpWebRequest) WebRequest.Create (\"https: \/\/api.itscope.com ...\"); <br><br>  MyHttpWebRequest.UserAgent = \"applicationCompany-applicationName-applicationVersion\"; <br><br>  HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse (); <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4\"><br>Examples C ++<\/h2>\n\n\n\n<p><strong>HttpWebRequest<\/strong><\/p>\n\n\n\n<p>Set the user agent variable in the HttpWebRequest to the name of your company and application<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  HttpWebRequest ^ myHttpWebRequest = (HttpWebRequest ^) (WebRequest :: Create (\"https: \/\/api.itscope.com ...\")); <br><br>  MyHttpWebRequest-&gt; UserAgent = \"applicationCompany-applicationName-applicationVersion\"; <br><br>  HttpWebResponse ^ myHttpWebResponse = (HttpWebResponse ^) (myHttpWebRequest-&gt; GetResponse ()); <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5\"><br>Examples Visual Basic VB<\/h2>\n\n\n\n<p><strong>HttpWebRequest<\/strong><\/p>\n\n\n\n<p>Set the user agent variable in the HttpWebRequest to the name of your company and application<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> Dim myHttpWebRequest As HttpWebRequest = CType (WebRequest.Create (\"https: \/\/api.itscope.com ....\"), HttpWebRequest) <br><br>  MyHttpWebRequest.UserAgent = \"applicationCompany-applicationName-applicationVersion\" <br><br>  Dim myHttpWebResponse As HttpWebResponse = CType (myHttpWebRequest.GetResponse (), HttpWebResponse) <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Examples Java<\/h2>\n\n\n\n<p><strong>Apache HttpGet<\/strong><\/p>\n\n\n\n<p>Set the user agent variable in the HttpGet&nbsp;to the name of your company and application<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">HttpGet myHttpWebRequest = new HttpGet(\"https:\/\/api.itscope.com....\");<br><br>myHttpWebRequest.setHeader(\"User-Agent\",\"applicationCompany-applicationName-applicationVersion\");<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>User agent What is a user agent? See&nbsp;https:\/\/en.wikipedia.org\/wiki\/User_agent&nbsp;for a detailed description. The ITscope Loadbalancer that accepts the API requests has some protection mechanisms for DoS, Flooding and search engine queries. &#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"kbtopic":[180],"kbtag":[],"class_list":["post-2451","kb","type-kb","status-publish","hentry","kbtopic-api-basics"],"_links":{"self":[{"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/kb\/2451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/comments?post=2451"}],"version-history":[{"count":1,"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/kb\/2451\/revisions"}],"predecessor-version":[{"id":2452,"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/kb\/2451\/revisions\/2452"}],"wp:attachment":[{"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/media?parent=2451"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/kbtopic?post=2451"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/guide.itscope.com\/en\/wp-json\/wp\/v2\/kbtag?post=2451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}