No response from cURL function - php

I have written the following function, which was code I used somewhere else and modified slightly to work as a function (using $url in function parameters):
function curl2str($url) {
$cURL = curl_init($url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($cURL);
curl_close($cURL);
return $data;
}
I simply want a function to return a URL into a string, for a quick and easy API. The URL I am passing it is valid and works fine when I put it into a browser. I am calling it like so:
<?=curl2str("**valid URL here**");?>
For some reason it is just returning false. What am I doing wrong?
update
When I put this questions URL into as $url, I get a response. But when I use my custom URL, which works fine in the browser, and simply display's a list of files in the directory, I get bool:false.
update 2
It would seem that any domain works fine, apart from the one that I am trying to access. It just so happens that this is a root domain on the same server, I am running this script from a subdomain, but because of basedir_restrictions I cannot access a folder from the subdomain. So I wrote a little php to get the contents of the folder, and output it to the browser as a serialized array (JSON is not installed). But I cannot get a response from this root domain at all. It works fine in the browser, just not in cURL. And everything else works fine in cURL.
:(

Try this code into your function:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if(curl_errno($ch))
echo 'Curl error: '.curl_error($ch);
curl_close ($ch);
Note: curl_errno($ch); return error number>0 if any error occurs from cURL and use curl_error($ch); to see what is the error from cURL.

I use this function:
function curl($url, $cookie = false, $post = false, $header = false, $follow_location = false, $referer=false,$proxy=false)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_location);
if ($cookie) {
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$response = curl_exec ($ch);
curl_close($ch);
return $response;
}

Related

PHP - cURL get's old version of google

When getting the Google site in cURL PHP I get the old Google page (The one with the blackbar and weird buttons). So is there a way to get the Google as it appears on google.com? My code:
function file_get_contents_curl($url, $proxy) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo file_get_contents_curl('https://www.google.com/', 'proxy:8080');
Thanks, if there is no way I am ok with the weird looking Google.

PHP curl returns error: Couldn't resolve host 'www.xxx.com'. How can I solve this?

this function returns null and prints error :
Couldn't resolve host 'www.xxx.com'
function file_get_html_using_cURL($url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
echo "\n--------------------\n";
print_r(curl_getinfo($ch));
echo "\n--------------------\n";
}
$output = str_get_html($output); // <-- Important line to convert string into object!
curl_close($ch);
return $output;
}
When I load this code in localhost, it works. But it throws the error when I upload it to the remote server.
I am guessing that the remote server has blocked CURL from executing.
It's a free server so I can't change any settings in php.ini
Is there any way around this?
P.S.
file_get_content($url)
is blocked as well. So I tried using curl instead. But not curl doesn't seem to work anymore.
Please check following code and verify your server issue.
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_URL, 'YOUR_URL' );
curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
var_dump(curl_exec($curl));
var_dump(curl_getinfo($curl));
var_dump(curl_error($curl));

Curl_exec return null in php

I have a problem to get the data using curl operation. Here i hide the token, If i use the url only in my browser then it returns the data but here its null.
<?php
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords?authtoken=".$token."&scope=crmapi";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>
Where i do mistake please help me.
This is how you can try with CURLOPT_RETURNTRANSFER which is used to return the output and curl_errno() to track the errors :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/my_url.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
Helpful Links: curl_errno(), curl_error()
Try adding these lines:
<?php
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("authtoken"=>$token,"scope"=>"crmapi"));
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>
please see below article , not just turn off verify , you should update your php.ini with pem file
https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
Anyone running a recent Linux distribution is probably already OK, because they get the curl libraries bundled in through their package managers and recent curl libraries come with the latest CA root certificate bundle from Mozilla.org.
For a PHP installation that doesn’t come with this file, like the Windows PHP distribution, you need to download the CA root certificate bundle and tell PHP where to find it.

cUrl - getting the html response body

I'm sure this is fairly simple. I'm using the function below to retrieve sites raw html
in order to parse it. during my testing, I decided to run my code on stackoverflow.com
Instead of getting the html response the Chrome is printing out the actual site rather then assigning the html to its veritable. What am I missing?
function get_site_html($site_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
global $base_url;
$base_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
return $response;
}
The site raw html should be assigned to $response, and then return it.
Your code works. Try echo htmlentities($response); You'll get the raw html for the site you're curling.

get the value of an url response with curl

I am using PHP curl method to get a string type response. To create the request I use:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, $data);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if($response === false)
throw new Exception(__CLASS__."::".__FUNCTION__."_".$err);
return $response;
Why I always receive a bool(true) response instead of the string I echo from the other side?
Thanks
Since you already have
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
in your code. curl_exec should already returns the content of the page instead of a BOOL.
This is a snippet of a library I use. As pointed out this might not be needed but it helped me out once...
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):
http://osdir.com/ml/web.curl.general/2005-07/msg00073.html
http://curl.haxx.se/mail/curlphp-2008-03/0072.html

Categories