-1

I have a php function that takes a URL from a user, checks if that URL is valid and exists, and then grabs the title of that URL. This works fine, except for URLs that redirect (eg : http://tintin.com redirects to http://us.tintin.com. In these cases, the function fails.

I then read that I need to add the following option -

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

I have added this, and yet the function fails, but I get no errors that pertain to this. Is there any proper way to debug this, or is there some other option I need to set apart from this, to get it to work?

Sainath Krishnan
  • 2,089
  • 7
  • 28
  • 43
  • Are you using `safe_mode` or `open_basedir`? – showdev Jun 05 '14 at 21:47
  • This has nothing to do with the redirect, `curl_exec` returns `FALSE` which means that there was an error. Check for that error. @Martin Konecny also gives you the hint what that error is. – hakre Jun 05 '14 at 22:00
  • Nothing to do with redirect? How hard is it for you to understand that EVERY OTHER LINK WORKS. ONLY the links that redirect to another link does not work. Seriously, Go find someone else to troll. – Sainath Krishnan Jun 05 '14 at 22:31
  • @MartinKonecny The last time I did that I had to delete the question due to someone who misread my question, marked it as a duplicate, and failed to provide any constructive criticism. – Sainath Krishnan Jun 05 '14 at 22:34
  • I wouldn't say the marked question is a duplicate. It shows how to debug CURL errors, but doesn't address a specific error. – showdev Jun 05 '14 at 22:39
  • @showdev: OP accepted the answer below that actually shows exactly that curl error which is comparably the same as the linked duplicate. – hakre Jun 05 '14 at 22:42
  • @SainathKrishnan: I wonder who trolls here. As the answer you've accepted shows, this has nothing to do with redirects. Btw, the link you've provided here does not even redirect in a normal browser. And if you would have followed the constructive criticism in comments to your previously posted question, you would have seen that you have been directly pointed to debugging resources to find that out easily and quickly. Additionally you have [deleted your question](http://stackoverflow.com/q/24068179/367456) on your own consent and decided to post again (this time with the URI in question). – hakre Jun 05 '14 at 22:46
  • @hakre Incorrect. This is a site where (atleast I thought) I can come for guidance when I run into an error I do not know how to debug. I ran into this particular error, I found a solution which did not seem to work, and hence I posted here. The person who answered actually took time to see what the problem might be, and identified an angle I had not considered yet. That is called answering a question. What you are doing is counter productive to the purpose of stackoverflow. We have rules here to ensure we lack spam, not to enforce it with a strong arm and point out technicalities. – Sainath Krishnan Jun 06 '14 at 00:01
  • @hakre And regarding not using your advice, I did infact use error reporting (`verbose` involved traffic monitoring, which I do not know about), and as I mentioned in this post, it showed me no errors. I possibly did the error reporting incorrectly, but that does not eliminate the fact that I genuinely made some effort to solve this by myself before I considered turning to this site for help. I would appreciate it if you respect that. – Sainath Krishnan Jun 06 '14 at 00:02
  • @hakre Also as the solution turned out to not be related to curl in this case, I don't see how this can be a duplicate of a curl issue. – Sainath Krishnan Jun 06 '14 at 00:03
  • @SainathKrishnan: Which brings back to the point that you had problems to decipher the error message you (in the orignally question) did ask about which was covered by the error reference. Also you missed to share the URI otherwise it could be pretty easy shown to you that that URI does not redirect (e.g. by telling you that putting it in the browser does not redirect). So you share with much words that you need to ask on Stackoverflow because it didn't come to mind to try the URI in question? Can happen, however does it qualify for SO? I highly doubt. – hakre Jun 06 '14 at 05:28
  • @SainathKrishnan: *And* in previous comments with links also the answer was (cross) referenced that tells about how to check [`curl_exec()`](http://php.net/curl_exec) return value. It just means you need do some reading your own instead of getting input served to you. That's all. This btw. shows how this is a "curl issue" esp. as you're making it as one (see your question title and description incl. the code). – hakre Jun 06 '14 at 05:29
  • I added in an edit literally 20-30 seconds after the question was posted, when I realised I forgot to include the actual question. You chose to ignore this and continued to treat the question as a different problem. – Sainath Krishnan Jun 06 '14 at 05:36
  • I've always taken all your edits into account. Keep in mind that future users looking for curl redirection are looking for curl redirection - so they won't find this useful. Same for those looking for problems resolving hostnames in DNS with curl - they won't search for curl how to follow redirects. That's just the point you seem to miss here. Your problem was to understand that `curl_exec` returns `FLASE` on error. You didn't see that. Instead you located your problem with following redirects which it *never* was (there wasn't even a redirect with that URI in the browser). – hakre Jun 07 '14 at 09:44

1 Answers1

1

I think the problem may be that http://tintin.com doesn't work.

$ curl -v tintin.com    
* Rebuilt URL to: tintin.com/
* Hostname was NOT found in DNS cache
* Could not resolve host: tintin.com
* Closing connection 0
curl: (6) Could not resolve host: tintin.com

You need to use http://www.tintin.com (which does work) and then your code should get and follow a 302 redirect to http://us.tintin.com

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159