I'm sending a rather long URL with cURL and I'm almost positive that it's too long for cURL to handle. The URL is http://hiscore.runescape.com/index_lite.ws?player= and after the ?player= paramter, there can be up to 12 numbers/letters/symbols.
Is there an alternative to cURL which would support long URLs like that, or could I use cURL with that long of a URL somehow?
There is no limit on the length of URLs with libcurl or PHP cURL. So this is a non-issue.
What leads you to believe that there are size limits?
symbols could be the possible cause
use some thing like
urlencode
base64-encode
function getStats($username) { echo $username // to see if username is being sent to this function
just run this code as a standalone to see if it works
function getStats($username) {
$ch = curl_init();
$data = array('player' => '$username');
curl_setopt($ch, CURLOPT_URL, 'http://hiscore.runescape.com/index_lite.ws');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
}
getStats('what_ever_username');
Related
$url = "http://www.reddit.com/r/{mysubreddit}/new.json";
$fields = "sort=new";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
{mysubreddit} is whatever subreddit I wanna check. It works fine to just grab that url via postman, or even in the browser. But when I use PHP/CURL, it returns empty. I've tried replacing the URL, with another URL to another site, and it works fine, so the curl part is working fine.
Is there something with reddit that I have to set? headers? or explicitly tell it for JSON? Or what?
I thought it might have to do with POST, but I tried GET to, still empty/null.
$url = "http://www.reddit.com/r/{mysubreddit}/new.json?sort=new";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
That doesnt work either
You just need to add:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
As others have mentioned, reddit is sending you a 302 redirect to https. You would be able to see that by examining the headers returned by curl_getinfo().
Enabling redirect following, as sorak describes, will work. However, it's not a good solution - you will make two HTTP requests on every single API call. This is a completely unnecessary waste of network and increases the execution time of your script. Instead, just change the url that you're requesting to be from https://www.reddit.com/ in the first place.
I have function for converting files and I want to run it as background process cuing curl.
$url = sendMessages.php;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$curled=curl_exec($ch);
curl_close($ch);
I used above code but didn't work.please advice?
Do I have to use full path url?
yes the url is expected to be a full url, that may look something like
$url = 'http://localhost/file/convertfiles.php';
the actual url really depends of your setup,
also you would probably be confronted to timeout issues, these can be overcome but I don't know enough about what you are trying to do or your setup to tell you how
So I'm trying to have users verify that they own the domain. So I generated a file and have them upload it to their site. So I then have to verify it, so what I do is
file_get_contents($url.'/'.$token.'.html');
All this returns is
bool(false)
Here's more of the code
$url = $_POST['url'];
//Get site info
$gin = $con->prepare("SELECT * FROM verify WHERE url = :url");
$gin->bindValue(':url', $url);
$gin->execute();
//Get token
$t = $gin->fetch(PDO::FETCH_ASSOC);
$token = $t['token'];
$url = $t['url'];
//Get content
var_dump(file_get_contents($url.'/'.$token.'.html'));
I have 3 columns in the table token, which is the string in the file. url which is the url obviously, its in example.com format. And a verified column which is either 1 or 0. Any ideas?
Based on my experience with fetching third-party content from more than 1 million domain names, I would not recommend you to use file_get_contents() because this PHP function cannot handle page redirects, site that requires a valid user-agent etc. The issue you are experiencing might be specific to a certain domain names only. A better approach to your problem is to use curl.
function download_content($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Firefox 32.0");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Usage:
$returned_content = download_content('http://stackoverflow.com');
Try to add http:// before $url and put a valid url. It will work
var_dump(file_get_contents('http://'.$url.'/'.$token.'.html')); // With a valid URL
If you enable error_reporting(E_ALL), then you will probably see that the use of HTTP URLs are disallowed due to an ini setting.
Warning: you are possibly opening a hole by allowing arbitrary prefixes in file_get_contents. Try to use parse_url to validate that you actually have a HTTP URL. Then you should probably consider using cURL and disable external redirects (otherwise one could pass a URL such as http://bit.ly/something# and still pass your tests).
Hi I am new to php and want to know some alternate function for the header('location:mysit.php');
I am in a scenario that I am sending the request like this:
header('Location: http://localhost/(some external site).php'&?var='test')
something like this but what I wanna do is that I want to send values of variables to the external site but I actually dont want that page to pop out.
I mean variables should be sent to some external site/page but on screen I want to be redirected to my login page. But seemingly I dont know any alternative please guide me. Thx.
You are searching for PHP cUrl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Set the location header to the place you actually want to redirect the browser to and use something like cURL to make an HTTP request to the remote site.
The way you usually would do that is by sending those parameters by cURL, parse the return values and use them however you need.
By using cURL you can pass POST and GET variables to any URL.
Like so:
$ch = curl_init('http://example.org/?aVariable=theValue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Now, in $result you have the response from the URL passed to curl_init().
If you need to post data, the code needs a little more:
$ch = curl_init('http://example.org/page_to_post_to.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
$result = curl_exec($ch);
curl_close($ch);
Again, the result from your POST reqeust is saved to $result.
You could connect to another URL in the background in numerous ways. There's cURL ( http://php.net/curl - already mentioned here in previous comments ), there's fopen ( http://php.net/manual/en/function.fopen.php ), there's fsockopen ( http://php.net/manual/en/function.fsockopen.php - little more advanced )
I want to read a server's reply for a certain request, modify it to my needs, and send it to the site visitor. get_headers() works perfectly for the headers, but if the requested file is missing (404), and that's exactly what I want to use, get_file_contents(), readfile() and other functions I've tried all break with the warning/error that the file is missing instead of reading the replied stream into a variable.
So what I want is a function similar to get_headers() only for the rest of the data, like a get_data() that doesn't cancel. Is there such a thing?
Thanks for reading.
Use curl_exec. It will always return the body unless the CURLOPT_FAILONERROR option is set to TRUE.
Here's an example:
$url = 'http://www.example.com/thisrequestwillerror';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
// This is the default, but just making sure...
curl_setopt($curl, CURLOPT_FAILONERROR, false);
// Execute and return as a string
$str = curl_exec($curl);
curl_close($curl);
// Dump the response body
var_dump($str);
Wrap this in a function and use it wherever you need to get an HTTP response body in your application.