I'm constructing a brandchecker for my internship. One of the parts of it is where you enter a name, and the checker checks on Twitter if the username is taken or not. This is the code I made (file_get_contents doesn't work on my server):
<?php
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, "https://twitter.com/users/username_available?username=".$z);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$twitter_contents = curl_exec($ch);
curl_close($ch);
echo $ch;
?>
$z
is defined above the code where I check Facebook for the username. It's defined as whatever people enter as their potential company name.
echo $ch;
outputs Resource id #3. Everywhere, people are talking about how this is not an error, but a MySQL result that needs to be pulled through a var_dump()
. But as you can see, I'm not pulling the data from MySQL, but from Twitter.com.
The Facebook checker uses cURL aswell, and it works flawlessly. And yes, I have switched out $ch
with other variables.
Does anyone have a solution to my problem?
Edit: I just tried echo(var_dump($ch));
, and it returned resource(3) of type (Unknown)
You're echoing the wrong result. In your code you're loading the result into $twitter_contents
by using the following code:
$twitter_contents = curl_exec($ch);
If you would change your echo to this, it should work:
echo $twitter_contents;
To explain my answer a bit more, $ch is your Curl which is a function that's not returning the actual page. It's returning a resource, the curl_exec
gets the actual content of the page and loads the result into a variable. Which you then can use to do other things.
Okay, after some comments, I came up with this and it worked... kinda..
$url = "https://twitter.com/users/username_available?username=test";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result['contents']=curl_exec($ch);
$result['info']=curl_getinfo($ch);
curl_close($ch);
echo $result['contents'];
We were missing the 'CURLOPT_SSL_VERIFYPEER' which twitter needs.. apparently.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.