I have this piece of code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$header[] = "Accept: text/html, text/*";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$html = curl_exec($ch) or die('111');
curl_close($ch);
echo $html;
exit('1');
I have windows with latest version of Xampp installed with curl enabled.
When I run the code it returns 111 without any errors.
I was expecting it to return the page content and 1
Try something like the example from the manual:
if(($html = curl_exec($ch)) === false)
{
echo 'Curl error: ' . curl_error($ch);
die('111');
}
curl_close($ch);
echo $html;
exit('1');
http://php.net/manual/en/function.curl-error.php
Don't die with a fixed error message. Try
$result = curl_exec($ch);
if ($result === FALSE) {
die(curl_error($ch));
}
instead. have curl tell you WHY it failed, instead of just spitting out some pointless 111 text.
Related
Curl stop working on my server but it is working perfectly on the other server
Here is my script:
<?php
$proxy_ip = '165.139.149.169';
$proxy_port = '3128';
$url = 'https://www.google.com/search?q=skinny+fiber+ingredients&btnG=Search&client=ubuntu&channel=fs&num=100&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=sw9CVbCuPKaA8QfX0ICYBA&gws_rd=cr';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.google.com');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0');
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // Good leeway for redirections.
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
$data = curl_exec($ch);
curl_close($ch);
echo "abc";
echo $data;
?>
http://serpbull.com/dev/curl.php (script not working here)
http://imarkdev.com/serp/curl.php (script working here)
Edit:
Not working script takes very long to respond (curl timeout) and prints empty response.
Use curl_error() function to get more information about the failure.
$data = curl_exec($ch);
if ($data === false)
{
echo 'Curl error: ' . curl_error($ch);
} else {
echo 'Response: ' . $data;
}
I am trying to get the download link from savefrom.net website, I am using the following code :
<?php
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$url = 'http://en.savefrom.net/savefrom.php';
$ckfile = dirname(__FILE__)."/c.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data['sf_url'] = "http://www.dailymotion.com/video/x34uoat_hero-behind-the-scenes-part-1-making-of-the-trailer_shortfilms";
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_REFERER, 'http://en.savefrom.net/');
$output=#curl_exec($ch);
$info = #curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($output, 0, $header_size);
$body = substr($output, $header_size);
//echo "<h1>body</h1><br>".$body . "<h1>Header</h1><br><br>" .$header ;
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
echo "output is:'" . $output . "'";
echo "<br>";
echo "<h2>info is:</h2>";
print_r($info);
?>
so far the code runs but it gives me alert messagbox which says : goto the website for direct download link!!!. Am i doing something wrong in the code. Is there any alternative way to get video download link from website like dailymotion etc. Thanks for your time
regards
This question already has an answer here:
Curl request showing different content from that of browser request
(1 answer)
Closed 9 years ago.
When I type a URL into my browser, it returns detailed output. However, when I try to do this by a curl request, the request returns a single empty blank space. Why this is happening?
URL is https://api.500px.com/v1/users?oauth_token=AihBz6ZWedu3VxnQdy2tqWtbwV86wtOuXumhPapk&oauth_verifier=YhKo0kaGhfw0dFhparxU&consumer_key=0OvWThqr5j1ZYX1cPaa8y0y1aOfJBbDtpX85fJ42
My code is:
<!DOCTYPE html>
<?php
function fetchData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
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_URL, $url);
$returned = curl_exec($ch);
echo 'Errors: ' . curl_errno($ch) . ' ' . curl_error($ch) . '<br><br>';
curl_close ($ch);
echo $returned;
return $returned;
}
);
// Pulls and parses data.
$returned = fetchData("https://api.500px.com/v1/users?oauth_token=xElRwQ6cqItG8Siy9kFBpwkj5sCdlp33NRva5TZU&oauth_verifier=hbNdYnqm8BSyuiZYa4KZ&consumer_key=0OvWThqr5j1ZYX1cPaa8y0y1aOfJBbDtpX85fJ42");
var_dump($returned);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
Please use CURLOPT_HEADER to check the response headers for any errors.
Use curl in following format -
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,'client='.$number.'&order='.$orderId);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
$data =curl_exec($ch);
I've been using PHP curl to get data I need from remote website. Here is the cURL function I used:
function get_content($adr)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $adr);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
$myrul = "http://remoteurl.com";
$result = get_content($myrul);
But how do I get the headers for the response?
If my comment above is correct, change:
curl_setopt($ch, CURLOPT_HEADER, 0);
to:
curl_setopt($ch, CURLOPT_HEADER, 1);
and parse the returned headers out however you see fit. Note that just changing the above in your function will return both headers and content, so if you want only headers returned:
function http_head_curl($url,$timeout=10)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // in seconds
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
if ($res === false) {
throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
}
return trim($res);
}
I am trying to use curl with a proxy. I have the following code:
function getPage($proxy, $url, $referer, $agent, $header, $timeout) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result['EXE'] = curl_exec($ch);
$result['INF'] = curl_getinfo($ch);
$result['ERR'] = curl_error($ch);
curl_close($ch);
return $result;
}
$result = getPage(
'75.125.147.82:3128', // use valid proxy
'http://www.google.com/',
'http://www.google.com/',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8',
1,
5);
if (empty($result['ERR'])) {
echo $results['EXE'];
} else {
echo $result['ERR'];
}
The output of this script is 'couldn't connect to host'
Anyone know whats wrong?
You might check what the status code is of where you're connecting to:
$status = curl_getinfo($http, CURLINFO_HTTP_CODE);
Might give you an idea of what is going on.
well
delete this code
curl_setopt($ch,CURLOPT_HTTPPROXYTUNNEL, 1);
it works for me,I'm testing this code as well
good luck