curl php empty blank space [duplicate] - php

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

Related

curl stop working on my server

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

PHP Curl post [savefrom.net]

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

curl_exec is not working

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.

Check headers in PHP cURL server response

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

Php curl problem

<?php
set_time_limit(0);
$php_userid = "my yahoo id";
$php_password = "my yahoo password";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$reffer = "http://mail.yahoo.com/";
$LOGINURL = "https://login.yahoo.com/config/login?";
$POSTFIELDS = ".tries=1&.src=ym&.intl=us&.u=3jtlosl6ju4sc.v=0&.challenge=NZYhS1spj7zunoVhpd6KRNqaF5Kz&hasMsgr=0&.chkP=Y&.done=http://mail.yahoo.com&.pd=ym_ver=0&c=&ivt=&sg=&pad=3&aad=3&login".$php_userid."&passwd".$php_password."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
$result = curl_exec ($ch);
curl_close ($ch);
echo $result;
?>
Is there any error? I'm doing something wrong, but i can not find it. please let me know if you can see where is my error? i need to set up this curl class.
The POST data is malformed, it should be :
[...] "login=" . urlencode($php_userid). "&passwd=" . urlencode($php_password) . ""
Instead of
[...] "login".$php_userid."&passwd".$php_password.""
You should use urlencode to ensure that the data passed in POST data is properly sent and you where missing an = for the login and passwd value.
You should use curl_error to determine what is failing during the cURL process. You can see a list of cURL error codes here: http://curl.haxx.se/libcurl/c/libcurl-errors.html
$result = curl_exec($ch);
$error = curl_error($ch);
print $error;
curl_close ($ch);
I assume the .challenge field gets dynamically generated. Your curl request uses an invalid challenge and so the server blocks it.
$cookie = cookie.txt";
Insert after: curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); this: curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);

Categories