Curl request not working from same origin - 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...

Related

PHP cURL redirects to another URL

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

Wikipedia and cURL with PHP

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);

Know if a live domain would redirect to https or not

I'd been working out all night to know what function should I use (basically running around cURL and DOM).
I would always be given with domain only, sometimes SSL enabled, that is, say paypal.com. But going to paypal.com would redirect me to https://paypal.com which is great.
What I want is, given the domain, know if that website will redirect me to https or non-https
Here is the working I currently have, it is cURL, if you have any other idea that would be great. Actually, I would prefer solutions using DOM since I'd been using DOM in my entire script.
$a = 'paypal.com';
$b = "http://" . $a . "/";
$url = $b;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo "Original: $url <br>Final: $lastUrl";
exit;
Thanks!
You can check the port by $_SERVER['SERVER_PORT'] if you get port "443" it means URL is SSL enabled.
you can try like this
if(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT']=="443"){
$domain = "https://".$_SERVER['HTTP_HOST']."/";
}else{
$domain = "http://".$_SERVER['HTTP_HOST']."/";
}
echo $domain;
Hope it should work for you..

Php curl error Url not found on this server

I am working on core php.
I am requesting a URL by curl. This is working on my development server but same thing are not working on live server.
Below is my Code:
$url = "http://www.streamatemodels.com/login.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submitted=1&g=&sausr='.urlencode('username').'&sapwd='.urlencode('password').'');
$result = curl_exec($ch);
$error = curl_errno($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($result);
Please help me if anyone have solution.
Update
I checked this with new username and password but same things happen. Its working on my development server but not working on live and local host.
The following line:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submitted=1&g=&sausr='.urlencode('username').'&sapwd='.urlencode('password').'');
should be changed to:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submitted=1&g=&sausr='.urlencode($username).'&sapwd='.urlencode($password).'');
If it still doesn't work try to create the query-string outside of curl_setopt:
$params = 'submitted=1&g=&sausr='.urlencode($username).'&sapwd='.urlencode($password).'';
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
Another advise: it's better practice to use http_build_query($params)
UPDATE
I had the time to check the code, and it works fine on my local host (meaning: check your username/password!):

PHP cURL request to same server returning false

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

Categories