cURL shows blank page - php

my curl code show me a blank page
<?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
$core = explode('<!--- BEGIN -->', $result);
$main = explode('<!--- END -->', $core[1]);
echo $main[0];
?>
this code works fine on localhost, but not on server...

There can be several reasons behind your problem.
1) Change <? into <?php and see whether it works or not.
2) For a short test, run this code from your server and check whether it shows you the output or not.
<?php
echo "sabuj";
?>
3) Some site seek for useragent string on their website request. When they found no useragent, they use to return blank page. You can overcome this with setting an useragent like below.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
4) Another thing you can do is, access the server with ssh client(if you have any) and run the url with wget tool and see whether you can download your page or not.
wget "http://yoursite/page/blabla/...php"
5) Finally, run your curl code with verbose mode enabled. It will help you to debug your curl requests.
curl_setopt($ch, CURLOPT_VERBOSE,true);

Working Code...
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
Hope it helps...

Turn your error reporting all the way to 11.
error_reporting(E_ALL);
This will tell you a little more about the issue you are facing. The most common culprit when working with curl on a dev environment and moving to a server is a missing curl package.
You can check to see if you have curl installed by doing the following:
if(!function_exists('curl_version')) {
throw new Exception('Curl package missing');
}
Thus "PHP Fatal error: Call to undefined function curl_init() in..." is a very common error that is thrown.
Some additional debugging tips are to..
print_r the response of curl_exec($handle);
print_r curl_error($handle) this will give you a curl error code.
setup a curl proxy using set_opt and CURLOPT_PROXY, set the value to your ip:port, open the port 8888 on your router and install charles proxy. This will show you an exact printout of the request and response.

I've read the previous answers and it seems that your code is fine but problem is connectivity to remote server, please try this:
<?
//debug
error_reporting(E_ALL);
ini_set('display_errors', 1);
//debug end
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo result;
?>
what does it output ?
Notes:
Do you administer the other server?
if not, is there a possibility that your ip was blocked by the remote server?
Does script http://mysite/scripts/showsomething.php contains any errors ?
If you're able to edit it, please enable php errors on showsomething.php by adding the following code at the top of it :
error_reporting(E_ALL);
ini_set('display_errors', 1);

I've solved the problem by adding the following code.
The problem is SSL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
I hope it helps.

Related

php curl request over ssl fails abruptly

I'm running a curl request from a php script, very straight forward
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://url-here');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
This works fine over http but as soon as i use https code execution doesn't pass the curl_exec command and i can't see what the error is. The client side i just receive a 'Connection Reset'.
It's worth noting it does the same thing if i use https://google.com.
Also worth noting that running this from the command like works fine so seems to be an issue running from a php script.
I've added the usual CURLOPT_SSL_VERIFYPEER to false and still no joy.
The same code does however work from my local machine so seems to be isolated to the server.
It's an amazon ec2 instance if that makes any difference.
Welcome any ideas.
Many thanks
Figured this out, curl wasn't compiled with SSL. Did this and all began to work.

error: couldn't connect to host via CURL on same domain

I have a URL - http://www.xxx.xxx.xxx/
This URL, I'm able to open through the browser. I'm even able to call this URL via cURL from my machine, but when I'm calling it from the code on the same server and domain, it gives me an error - error: couldn't connect to host
Following is my code -
function get_xml_via_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
echo $url;
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
/*curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);*/
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
if(curl_errno($ch)) {
echo 'error: ' . curl_error($ch);
}
curl_close($ch);
// $xml === False on failure
$xml = simplexml_load_string($returned);
return $xml;
}
Like I said, on localhost this works. I can directly open the URL via the browser. But when I deploy the code on the same server and try calling via cURL, it gives me an error - error: couldn't connect to host
Updated Code -
I modified the code on my server to -
function get_xml_via_curl($url) {
$url = " http://www.xxx.xxx.xxx.xxx/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
if(curl_errno($ch)) {
echo 'error: ' . curl_error($ch);
}
curl_close($ch);
$xml = simplexml_load_string($returned);
return $xml;
}
And the error from curl changes to following -
error: Protocol http not supported or disabled in libcurl
Your updated code is not working because of the white space before http in $url = " http...
The original code is probably not working because of some firewall or nameserver issue. Try to visit the URL you want to load directly in a browser ON THE SAME SERVER. That will probably fail too.
You need to fix the server to accept requests from the localhost.
What can also help you on your way is to check the web server and firewall log files. See why the requests are being declined. And even temporarily just disable the firewall to rule it out.
I have faced the same, PHP curl throwing 'Connection timeout error' because of same domain and tried all other options, but nothing worked out.
Finally I got the answer and fixed it. Just run below command (ubuntu)
# sudo ifconfig lo up
For Cent OS:
# sudo ip link set lo up
# sudo ip addr show
"lo" is the loopback interface. This is a special network interface that the system uses to communicate with itself.
try:
$url="//your url";
echo get_xml_via_curl($url);

PHP CURL SSL Connecting to XML Gateway

This has been driving me crazy for 2 days - I have 2 PHP functions as detailed below both using curl. They point at exactly the same 'XML gateway' the only difference is one is trying to do it over SSL and the other over unencrypted HTTP.
The HTTP connector operator works exactly as expected, posting the XML file and returning the server response.
The SSL connector returns the ever so vague 'An internal server error occurred. Please try again later'. Nothing shows up in my lighttpd error log, has anyone got any bright ideas?
I'm wondering if it's my web server config/openSSL config. They are both Debian Wheezy standard packages. I appreciate SSL_VERIFYPEER & HOST being set to sale is insecure, however I've been trying to exhaust the options.
openssl s_client -connect xmlgw.companieshouse.gov.uk:443 -ssl3
and the command line function
curl https://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway
also works as expected on the web server.
PHP functions:
//SSL post function
public function getSSLCurlResponse($xml) {
$url = "https://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
//HTTP non SSL function
public function getCurlResponse($xml) {
$url = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
any help would be really appreciated!
Thanks
I have concluded this was an error in the overall connection to the server - though i could not find any way of proving that. I have managed to find an alternative solution without using an SSL socket.

cURL request using socks5 proxy fails when using PHP, but it works through the command line

cURL + proxy noob here, having a hard time. I'm having trouble trying to retrieve a web page from a remote secure server via a proxy. Everything has apparently been set up correctly by a remote dev, such that the following command line instruction works and returns what we're looking for:
curl -k --socks5-hostname localhost:xxxx https://hostname/
However, the following PHP does not echo the requested webpage. Instead it echoes the error 'Couldn't resolve host name':
$proxy = 'localhost:xxxx';
$url = 'https://hostname/';
//$proxyauth = 'user:password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
elseif ($curl_scraped_page)
echo $curl_scraped_page;
If the $url is changed to a public page, such as Google, the request is successful and everyone is happy.
The connection requires an SSH tunnel if that changes anything at all. The tunnel is open and functioning, as proven by the command line request succeeding.
Is there something obvious that is being missed here?
You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7:
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
In the option CURLOPT_PROXYTYPE you need to set CURLPROXY_SOCKS5_HOSTNAME option instead of CURLPROXY_SOCKS5.
In this case, the DNS query (for hostname resolving) will be sent to SOCKS proxy and not resolved in the local network.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
This constant available since PHP 5.5.23 and PHP 5.6.7 and cURL 7.18.0, so you can simply use it.

Using CURL, gives 500 Internal Server Error

I am using CURL and I am getting a 500 internal server error. I am not using the user agent option, could this be causing the issue?
This is the snippet
$current_url="http://localhost/mysite/entercode.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $current_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=abc&variable2=123');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
The same thing just happened to me.
A 500 Internal Server error when i tried to execute $ch = curl_init();. It turns out php curl wasn't installed on my server. I installed it and presto my code worked!
Check this out for how to install PHP CURL
entercode.php is throwing an error related to your input. I suggest you check the server error log. Your curl implementation is fine.

Categories