My curl post doesn't return a body on the server side. How can I rectify this?
curl_setopt($ch, CURLOPT_URL,$cpUrl);// "ex:www.lalala/aa/"
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $varpost); // has string parameter
curl_setopt($ch,CURLOPT_HTTPHEADER,array('VariableParams:'.$varpost ));
info ("variables passed:",$varpost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_NOBODY, false);
$output = curl_exec ($ch); // Execute
It can happen that for the server the request is redirected into another url. For example based on location some website redirects it to a particular location. That's why you are not getting any body as you are disabling the follow location option.
Try by enabling it.
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
Related
I'm having trouble understanding how CURL handles headers.
I have a site.com/page1 that I want to access with CURL it does a 308 redirect to site.com/page2/file.zip
What I need is to go through site.com/page1 with CURL but download site.com/page2/file.zip directly from site.com
I'm using this code but it does not work as expected. It accesses site.com/page1 redirects to site.com/page2/file.zip but opens the file in the browser
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_cookie_file);
curl_setopt($ch, CURLOPT_REFERER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);
$error = curl_getinfo($ch);
curl_close($ch);
I guess if I can keep the response headers I'll fix the problem. But how do I do it ?? How do I use the same headers for the CURL visitor that the site I am accessing is sending me.
You want the cURL option RETURNTRANSFER set to true so what is returned comes back to you. Since you are trying to save a ZIP file you'll also need to open a file and use the CURLOPT_FILE option to tell cURL where to save your ZIP file.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_TIMEOUT,'180'); # 3 minute timeout
$FileOut = fopen('MyZIP_File.zip','w') or die('Could not open the output data file');
curl_setopt ($ch, CURLOPT_FILE,$FileOut);
curl_exec ($ch);
fclose($FileOut) or die('We ran into a problem saving data file');
This solved the problem.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_cookie_file);
curl_setopt($ch, CURLOPT_REFERER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
if (preg_match('~Location: (.*)~i', $result, $match)) {
$location = trim($match[1]);
header('Location:' . $location);
}
I am doing a remote location, and I can successfully login, and follow the location of the url to the next page. But from there I need to go to another page(it does not automatically redirect), I need to do it manually. This is the link after the login and follow location https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork, and this is the link I need to manually redirect to https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$url = curl_setopt($ch, CURLOPT_URL, 'https://homeaccess.katyisd.org/HomeAccess/Account/LogOn? ReturnUrl=https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://homeaccess.katyisd.org');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$wrapperPage = (curl_exec($ch));
curl_close($ch);
print $wrapperPage;
?>
In this instance you can not redirect much like what would happen if you were using a browser. You'll have to do another cURL session, setting your URL as:
https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork
It's not the user which spawns the redirect, its the web server when it sends the HTTP response code 302.
I need to redirect the current user to an external page (other domain) with some parameters that will be used on that page. To do that I'm using cURL, an example code is below:
$postfields = array('key'=>'value');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain.com/example.aspx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
curl_close ($ch);
But the user stays on the same page and I don't know if any data is transmitted.
What is the problem here? Is there another way to do that?
I am attempting to login to Reddit using their login API - https://github.com/reddit/reddit/wiki/API%3A-login. I am able to successfully authenticate and store a cookie using
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.reddit.com/api/login/USERNAME');
curl_setopt ($ch, CURLOPT_REFERER, "http://www.reddit.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$postData = 'api_type=json&user=USERNAME&passwd=PASSWORD';
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_exec($ch);
but when I add
curl_setopt($ch, CURLOPT_URL, 'http://www.reddit.com/r/pics');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
echo $data;
to the end of the authentication code I receive the 'page not found' page from Reddit, even though I am succesfully logged in and the page seems to indicate that I am indeed in the /r/pics subreddit. I am wondering if there is some sort of redirect happening or if any options are missing or incorrect.
You'd still be performing a POST operation when you load the second URL. CURL will not switch 'back' to a GET unless you tell it to. Try adding in
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
In the below code i am trying to post the username and password but not able to create the cookie using post, if i do the same with get method it creates the cookie.
$BASEURL="http://localhost/openx/www/api/json/index.php/main/authenticate/";
$POSTFIELDS='username="'.$username.'"&password="'.$password.'"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
curl_setopt($ch, CURLOPT_URL, $BASEURL);
$returned = curl_exec($ch);
The curl_getinfo($ch) returns 200 OK http code, can anybody please advice what is wrong with the above code.
Thanks in advance.
The cookiejar is only saved when you close the curl handle using curl_close($ch).
From the manual:
CURLOPT_COOKIEFILE
The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. If the name is an empty string, no cookies are loaded, but cookie handling is still enabled.
CURLOPT_COOKIEJAR
The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.
http://www.php.net/manual/en/function.curl-setopt.php
I have changed my code to below and now working for me.
$ckfile = tempnam ("/tmp/", "CURLCOOKIE");
$BASEURL='http://localhost/openx/www/api/json/index.php/main/authenticate/';
$POSTFIELDS='username='.$username.'&password='.$password.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,'http://localhost/openx/www/api/json/index.php/main/authenticate/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
ob_start(); // prevent any output
$result=curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
$result = curl_exec($ch);
curl_close($ch);