I am using the curl function on my site if I use the url http://www.markettrendsignal.com/v1/market/header_cal.php it is showing the error not found else if I removed the www in front of the error it is working fine but I want the www in the url. How can I resolve it. I check the url it is working fine with www and without www. I am using the curl function in the same doamin.It is showing the following error.
"Not Found
The requested URL /v1/market/header_cal.php was not found on this server.
Apache/2.2.3 (Red Hat) Server at www.markettrendsignal.com Port 80"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.markettrendsignal.com/v1/market/header_cal.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
print_r($contents);
curl_close ($ch);
use CURLOPT_FOLLOWLOCATION on curl_setopt function. It will follow the link even in redirect. This is caused because for your htacces file. Domain hosted without www and you putting the link www so the page redirected as 301. See the available option http://www.php.net/manual/en/function.curl-setopt.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.markettrendsignal.com/v1/market/header_cal.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$curled=curl_exec($ch);
curl_close($ch);
print_r($curled);
It is working well on my side, please use this
Related
I have been using cURL code to access Beanstream for awhile now, and all of a sudden it stopped working as of yesterday. I have determined that this has to do with accessing the HTTPS URL for Beanstream processing, as if I test that same code and just go to the HTTP URL, it works (of course it returns an insecure connection code).
Prior to the below code I was setting VERIFYPEER / VERIFYHOST to FALSE, which was working up until yesterday.
$url = 'https://www.google.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, "/xxxx/public_html/_cert/GeoTrustGlobalCA_google.crt");
$getResponse = (curl_exec($ch));
curl_close($ch);
print_r($getResponse);
I have setup the above test code to try and access Google via HTTPS. I had read that a proper connection requires the VERIFYPEER / CAINFO options to be set, which I have done and grabbed the Google .crt and placed it on the web server as well.
I still get a 503 Service Unavailable error back though.
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Additionally, a 503 Service Unavailable error was encountered while
trying to use an ErrorDocument to handle the request.
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);
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.
iam using that php code in the localhost and working fine
$ch = curl_init($src);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
when i move it to the remote server the script ignores that code
and doesn't give me a result (still giving me the original url )
note: i use cURL here to give me the true url after redirect
Maybe there is some difference in the configuration of the remote server.
phpinfo();
Shows the configuration of php and of the curl module
I have a website hosted on my local server (http://localhost/), which uses url_rewriting.
When I access a page with the browser, no issue, it's loading.
But when I access the same url with PHP cUrl, I get a 404.
Example:
URL with mod_rewrite: http://localhost/site/test
Real page: http://localhost/site/test.php
Loading http://localhost/site/test from Firefox: Working
Loading using cUrl:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/site/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
$output -> 404 page from apache
Anybody knows why cUrl returns a 404 message?
Turns out this has nothing to do with mod_rewrite.
One of the directory in the URL had a space in it, that I was not encoding when calling the cUrl function.
Once replaced all space by %20, everything worked fine.
Subject closed.