title: How can I connect to a Tor hidden service using cURL in PHP?
body: <p>I'm trying to connect to a Tor hidden service using the following PHP code:</p>
  <pre><code>$url = 'http://jhiwjjlqpyawmpjx.onion/'
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_PROXY, &quot;http://127.0.0.1:9050/&quot;);
  curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  $output = curl_exec($ch);
  $curl_error = curl_error($ch);
  curl_close($ch);
  
  print_r($output);
  print_r($curl_error);
  </code></pre>
  <p>When I run it, I get the following error:</p>
  <blockquote>
  <p>Couldn't resolve host name</p>
  </blockquote>
  <p>However, when I run the following command from my command line in Ubuntu:</p>
  <pre><code>curl -v --socks5-hostname localhost:9050 http://jhiwjjlqpyawmpjx.onion
  </code></pre>
  <p>I get a response as expected.</p>
  <p>The PHP <a href="http://en.wikipedia.org/wiki/CURL" rel="noreferrer">cURL</a> documentation says this:</p>
  <pre><code>--socks5-hostname
  Use  the  specified  SOCKS5 proxy (and let the proxy resolve the host name).
  </code></pre>
  <p>I believe the reason it works from the command line is because Tor (the proxy) is resolving the .onion hostname, which it recognizes. When running the PHP code above, my guess is that cURL or PHP is trying to resolve the .onion hostname and doesn't recognize it. I've searched for a way to tell cURL/PHP to let the proxy resolve the hostname, but I can't find a way.</p>
  <p>There is a very similar Stack Overflow question, <em><a href="https://stackoverflow.com/questions/14944067">cURL request using socks5 proxy fails when using PHP, but it works through the command line</a></em>.</p>