Server ignores cUrl code lines php - php

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

Related

Can't load file from URL, but works with local path

I am trying to load an XML file, but for some reason I can't use a URL.
allow_url_fopen is set to On.
I used this, which didn't work:
$xml = simplexml_load_file("https://example.corn/test.xml");
But this does work:
$xml = simplexml_load_file("../test.xml");
I also tried file_get_contents:
$xml = file_get_contents("https://example.corn/test.xml");
and even cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.corn/test.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
curl_close($ch);
For each example I am displaying the output with var_dump($xml);, which always shows bool(false) when using a URL and shows the correct XML data when using a local path.
I would just use the local path, but this is just for a test and the actual XML file I am going to fetch is from a different site.
I tested it on a local server and it worked, but on my hosted server, it doesn't work at all. They are using the same php.ini. My hosted server is using php-fpm, I'm unsure if that could be a factor, or if there is something that has to be changed with it.
This might be a configuration error that I am unaware of, because the code should work, and the link definitley exists, because I can click on it and see the correct xml in the browser.
What could be causing this issue?
Turns out that I had my openssl.cafile property set to some location that didn't exist.
For some reason no errors were showing when using the commands above.
I think the specified url is not valid or doesn't exist
Read about curl in the PHP docs
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);

API not work on Third Level Domain

i have some problems.
I need to keep data from coinmarketcap. When I was developing on localhost it worked well.
But on third level domain coinfollow.altervista.org i can not receive the data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.coinmarketcap.com/v1/ticker/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
$outputdecoded = json_decode($output, true);
echo $outputdecoded;
I try in another domain mywebsite.com and it worked. I think that the problem is coinfollow.altervista.org domain.
I need to save coinmarketcap data into my database with a simple query.
Does anyone know a solution?
It sounds like there is a server config issue on coinfollow.altervista.org, such as curl not being enabled for php.
You can run phpinfo(); to see if curl is installed.
If it is installed try running echo curl_error($ch) to see if there are any errors returning from curl

PHP curl works on localhost but not on the web server?

The below php code is making a REST API call that returns json object. This works fine when i use it on my localhost using WAMP Absolutely no problem.
However when i push this app on the server it would TimeOut and display 503 Service Unavailable.
I checked the logs it has an entry :
The TimeOut Specified has expired.
I contacted my admin he just said this app listens to PORT=64665 and HOST=0.0.0.0. What does that mean? What more changes do i need to make in my code to make it work on the server ? Help
<?php
$url = "http://xyz.net/v2/plan/"; // I have changed the REST URI API Link for security reasons
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, false);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
// Will dump json
var_dump(json_decode($result, true));
$response = "http://xyz.net/v2/plan/";
echo $response;
?>
Check your server's /etc/hosts file, there could be the problem, ie. domain mapped to another (wrong) IP address.

CURL on localhost with MOD_REWRITE -> 404 (but fine in browser)

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.

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