I am trying to take a list of URL's from a textbox, it has 1 URL per line and each URL does a redirect, I am trying to get the URL that it redirects to.
When I run this code below on a single URL, it returns the redirected URL which is what I want...
function getRedirect($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
curl_close($ch);
echo '<pre>';
print_r($info);
echo '</pre>';
}
$url = 'http://www.domain.com/go?a:aHR0cDovL2xldGl0Yml0Lm5ldC9kb3d';
getRedirect($url);
Now my problem is when I try to run it on multiple URL's with this code...
if(isset($_POST['urls'])){
$rawUrls = explode("\n", $_POST['urls']);
foreach ($rawUrls as $url) {
getRedirect($url);
}
}
When I run it on my list of URL's instead of giving me the redirected URL like my first example does correctly, it instead gives me the URL that I passed into cURL.
Can someone help me figure out why or how to fix this please?
It's already covered in the question comments but it seems the problem would be extra spacing at the end of the url.
Calling getRedirect(trim($url)) would fix it.
The space at the end is most likely turned into a querystring space (aka %20) and changes the value of query string parameters
Related
I have the following php function:
function checkJQL($filter) {
$auth = "account:password";
$URL='http://jira/rest/api/latest/search?jql='.$filter;
echo $URL;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Tell it to always go to the link
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$URL);
//Set username and password
curl_setopt($ch, CURLOPT_USERPWD, $auth);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
echo $result;
// Will dump a beauty json :3
var_dump(json_decode($result, true));
echo "DONE";
}
When I call this with $filter = "" It works fine, outputs everything. As soon as I insert a proper JQL query, it fails. When I enter random garbage, it works (As in I get a invalid input message), but when it's proper JQL it never works.
When I copy and paste the URL that I echo into the browser it works.
An example filter I used:
"Target" = "Blah"
When I think about it, I don't actually need this to work, I just need it to know when the input isn't JQL (which it does). But I'm really curious now. Anyone have ideas on what it might be?
You should URL-encode the $filter.
The reason why it works with empty or random strings is because they don't have any challenging characters that need to be URL-encoded.
The reason why the URL works in the browser but not in the script is because the browser does the URL encoding.
So do the following:
$URL='http://jira/rest/api/latest/search?jql='.urlencode($filter);
Link to urlencode: http://php.net/manual/en/function.urlencode.php
I am using an API to get the users profile picture. The call looks something like this
https://familysearch.org/platform/tree/persons/{$rid}/portrait?access_token={$_SESSION['fs-session']}&default=https://eternalreminder.com/dev/graphics/{$default_gender}_default.svg
This link only works for about an hour because the user's session token expires then. I was wondering if there was any way to retrieve the last returned returned URL, which would be the direct link to the image, so I could store that in a database.
I have tried Google but I don't really know where to start.
Thanks in advance!
I was able to solve my own problem. It was doing a redirect to get the image and I just needed that URL. Here is my code that helped me get there.
$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
// Uncomment to see all headers
/*
echo "<pre>";
print_r($a);echo"<br>";
echo "</pre>";
*/
echo $url; // Voila
I had used this code to get redirected url in $last_url string :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.djgol.com/files/download/id/163799");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$last_url= curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
$output = curl_exec($ch);
echo $last_url;
curl_close($ch);
?>
but i am unable to get redirected url ,, please help me
You're trying to get the URL before starting the request. Try moving the $last_url= curl_getinfo($ch,CURLINFO_EFFECTIVE_URL); line to after the curl_exec($ch); call.
use
$_SERVER['HTTP_REFERER'];
to get last url. I use this only.
I use cURL to send requests to REST API and I would like to add a string to created url befeore execute it. This string is not a parameter.
Do you know how can I do that?
It's called string concatenation. In php it can be done: $str = $string1.$string2;
p.s. Always read documentation before asking something. There you can find answers on most of your questions.
It depends on really what you want to do. If you want to call an URL by appending GET query parameters, you can use string concatenation.
<?php
function get_data($url, $add) {
$url = $url.$add;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_data('http://example.com', '?page=mine');
?>
However, if you want to retrieve a specific URL by sending POST query parameters (e.g. to simulate a form being submitted), you need to implement something like this instead.
I'm using the following code:
$ch = curl_init('www.google.com');
$output = curl_exec($ch);
curl_close($ch);
This is not the first time I've used cURL, and unless I'm mistaken the above code should retrieve the contents of google.com and store it in $output. Correct?
So, why then, does the above code output the contents (in this example the Google homepage) to the page? I'm not echo'ing anything out, but for some reason the curl_exec() function is outputting what it returns to the page.
Am I missing something?
you need to use
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
That will tell curl_exec not to output the results
so change it all to
$ch = curl_init('www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);