I'm trying to load information from Wikipedia with cURL and PHP, but it's not working as intended.
See my code :
$url = "http://en.wikipedia.org/w/api.php?action=opensearch&format=xml&limit=1&search=stackoverflow";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'USERAGENT');
$result = curl_exec($ch);
curl_close($ch);
//return $result;
var_dump($result);
It doesn't return anything. However, if you load the address in your browser, it works!
So I'm wondering what should I change in order to execute this cURL request well.
Thank you folks !
The request is redirecting to https. So either reference the https uri instead or set CURLOPT_FOLLOWLOCATION to true:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Related
In my code when I try to fetch redirect using cURL, it won't show the new location but when I put the same URL in browser it redirects to somewhere else I don't know how without redirect rules. but I need to get that same URL in result.
Here is the example URL
http://www.dailymotion.com/cdn/H264-1280x720/video/x1r819x.mp4?auth=1505704620-2562-a9qmrjvz-6e0f4066eb3a57ce79c011f5c3f932f3
Here is the Redirect URL
https://proxy-23.sg1.dailymotion.com/video/907/091/106190709_mp4_h264_aac_hd.mp4?auth=1505539349-6658-b3dukan0-a41773a8fa2338e758ce74cff24b0390#cell=sg1&hls_heuristic=1&hls_startFragPrefetch=1
Here is my PHP Code
$url = "http://www.dailymotion.com/cdn/H264-1280x720/video/x1r819x.mp4?auth=1505704620-2562-a9qmrjvz-6e0f4066eb3a57ce79c011f5c3f932f3";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // HTTP request is 'HEAD'
$headers=curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $headers, $matches);
curl_close($ch);
$print_newurl = !empty($matches[1]) ? trim($matches[1][0]) : 'http://www.google.com.fr';
echo $print_newurl;
Try to follow the redirect with CURLOPT_FOLLOWLOCATION.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Edit:
$last_url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
If that doesn't work, then try with CURLINFO_EFFECTIVE_URL. See more http://php.net/manual/en/function.curl-getinfo.php
Hi i have a website say abc.com and there is a another site which is installed in the subfolder abc.com/site .When i am making a curl request from the abc.com/site to abc.com it is showing 302 .
below is my code
$ch = curl_init();
$data['username'] = 'abc';
$data['password'] = 'abc';
curl_setopt($ch, CURLOPT_URL, "abc.com/site ");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$a = curl_exec($ch);
print_r($a);
but when i am making the same request to the production site from the dev site it works well. It seems like it is not working on the same origin. Please suggest how can i fix this
302 is a redirect and you have explicitly told curl to not follow redirects with CURLOPT_FOLLOWLOCATION set to false...
I am trying to execute a robots.txt HTTP request. For that I am using PHP and curl. This is the code I tried:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "robots.txt");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.stackoverflow.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;
Nothing gets printed to the screen. Any idea what am I missing?
In CURLOPT_URL you must write full host and path
For example: http://www.stackoverflow.com/robots.txt
I've searched high and low for the answer to this question but cant find anything...
We've got a single server and on it we've got a PHP service with an API.
We've recently written an PHP app which interacts with the API. When it goes live the API and the app will be on the same server.
However, when they are on the same server the cURL requests from the app to the API always return false. I'm sure this must be something to do with the way that the request is being routed by the server. Is there any way to make this work properly?
$url = 'http://api.some_address_on_the_same_server.com';
$postdata = array(...);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); // $result is always false when on the same server for some reason
curl_close($ch);
Must be related to locking session situations. Try this way.
<?php
$url = 'http://api.some_address_on_the_same_server.com';
$postdata = array(...);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
session_write_close();
$result = curl_exec($ch); // $result is always false when on the same server for some reason
curl_close($ch);
session_start();
?>
EDIT :
Have you added an exception in your windows host file ?
/windows/system32/drivers/etc/hosts
like
127.0.0.1 yourdomain.com
For more information. Check out this
I am trying to connect to an API using cURL and https and I don't get it work.
It work perfectly with http but when i change it to https in the $url variable, it takes more than 30 seconds to execute and finally it shows false as a result.
This is the function:
$url = 'https://api.xxxxxxx.com/api.php?user=xxxx&pass=xxxx';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
If I debug with this:
debug(curl_init($url));
I get:
resource
Any idea which could be the cause?
It ended up to be that the website was trying to access didn't allow secure connections.