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);
Related
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
I want to upload images from my Server to another Server with php curl. I tested my script on localhost it works but on server it not working.
Here are my steps:
First step
I upload an image to my server. Path of the image is "imgs/myimage.jpg"
Second step
I excute this code for upload image from my server to another server.
$file_name_with_full_path = realpath("imgs/myimage.jpg");
$postdata = array('myfile'=>'#'.$file_name_with_full_path);
$headers = array("Content-Type:multipart/form-data");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, "http://www.secondserver.com/image.php");
$data = curl_exec($ch);
curl_close($ch);
This code in image.php from second server.
print_r($_FILES["myfile"]);
I tested this code on localhost it works. But on Server it not working and display error.
Notice: Undefined index: myfile in ....
I try to get full path of the image on the server with command
echo realpath("imgs/myimage.jpg");
It shows this:
#/home/admin/domains/mydomain.com/public_html/member/imgs/myimage.jpg
I'm not sure what it is about Permission? But I have also set up file permissions are 777.
What should I do to fix this problem?
I'm trying to find a solution for the entire day, but was not successful.
Update
I apply the code to test on another server. It worked.
I try to get full path of the image on the server with command
echo realpath("imgs/myimage.jpg");
It shows this:
#/var/www/vhosts/myuser/mydomain.com/imgs/myimage.jpg
I think probably about Config Server.
Should I set up?
Not sure if you updated code for posting here but $postdata is never set before. In your 2nd line, you have $postimg = array('myfile'=>'#'.$file_name_with_full_path);
and later you do curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
Please use the correct variable ($postimg) and you should be good to go. Wondering how it worked on local :)
I'm try to save some pictures from url to my server, but i'm not able to do it.
this is my code (it's standard, i've found on internet):
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
but for each link i put inside $url variable, on my server is always saved a 26 byte image (that's not the original image).
What's wrong?
I can successfully download image using your curl code. It can happen that your server is not allowed to connect the outside web links.
This is a curl equivalent code that download images as well. I believe from your server, you can not download image using this code.
file_put_contents("img.jpg", file_get_contents("http://www.letteratu.it/wp-content/uploads/2014/03/cielo.jpg"));
Run your curl with verbose mode to see the curl's debug messages, and show that us.
curl_setopt($ch, CURLOPT_VERBOSE, 1);
I'm pretty sure you need to include the http:// in the URL. I'm fairly certain it thinks that it is a local file without it (i.e., an implicit file://).
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 problem with my server setting I guess. I have a code which fetches code from another server. I don't know why, but the code is not working with my VPS, but it is working with a simple shared hosting account...
Here is the code:
$post = array(
'KEY' => 'somekey',
'format' => 'xml'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'xxxxxxxxxx.com' . http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$xml_obj = simplexml_load_string($response);
$json = json_encode($xml_obj);
$array = json_decode($json, true);
curl_close($ch);
It is working like a charm if I run it on my shared hosting server, but it just have some problem (which it does NOT output) and the script does not run. I have tested that I got 1 (true) for this statement: $json = json_encode($xml_obj); But not for any further code. So there might be somewhere the the problem.
I have also checked if xml DOM is enabled, and it is. I have also checked CURL and json, and both of them are enabled.
Can someone help me? I can't do anything without error messages, and I cannot figure out what could be the problem. :/
It seems like you may have remote allow_url_fopen not enabled.
Put this at the beginning of your script:
ini_set("allow_url_fopen", 1);
To enable curl_exec, you need to modify your php.ini and remove it from the last of disallowed functions. To find out your php.ini, you can call this at the beginning of your script once:
phpinfo(); die();
That will tell you which php.ini file to use (look for the php.ini string). Once you modify php.ini, you will need to restart your web server.