How do I transfer data to next server - php

I am trying to pass some information of a server in which the script is running to next url or server.
I tried using curl. I have following two disadvantages:
if cannot locate file it will tell file not found
it waits till the remote file execution is completed.
How can I overcome both of the things either by using curl or other commands?
Edit:
Now I would like to suppress the file not found message error message being displayed by curl even if the file really doesn't exists.
I do not want output from destination page so I don't want to wait till the execution of the destination page is finished. I just want to trigger the code and continue with another scripts remaining
Example:
I am trying to build a log system which will have its everything in next webserver. The client website which implements the log system will be sending some of the data required for the system by calling a file in my webserver.
Code I am using:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://example.com/log.php?data=data");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);

Why don't you want to use standard PHP features?
<?php
$logpage = file_get_contents('http://example.com/log.php?data=data');
echo $logpage;
?>

Related

How to download file to server dynamically using php

I was curious to know if there was a way to download a file from SERVER A and put it on SERVER B where SERVER A has the ability to dynamically change what's in the downloaded file.
The point behind it is that I'm trying to build an error handler for a tool that will be used when a file that is a needed part of a tool goes missing. It would be like WordPress realizing there is a file missing on your site and your site sends a request to wordpress.com to get the missing files like this:
(SERVER B): PHP spits out error on include
(SERVER B): PHP tries to get a file installer for the missing files from SERVER A by saying SERVER B is missing FILE A, FILE B, FILE C, etc...
for the step above I was thinking it could be done using this:
file_put_contents("missing_installer.php", "http://SERVER_A.com/mi_inst_installer.php?query-asking-for-missing-item(s)=missing-item", 'r'));
NOTE (only if you don't understand what the above code does): The above code is supposed to tell SERVER A's PHP file, mi_inst_installer.php, to spit out data (the installer) and put it into file, missing_installer.php, on SERVER B
(SERVER B): PHP installs missing files using the newly obtained missing_installer.php
Any ideas on what to do?
You can also use cURL for dynamics on SERVER A's Side:
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$return = curl_exec($ch);
curl_close ($ch);
return $return;
}
file_put_contents('missing_installer.php', curl('http://SERVER_A.com/mi_inst_installer.php?query-asking-for-missing-item(s)=missin-item'));
Jacky's answer is good only if allow_url_fopen is set to 1.
also use PHP's reference for cURL transfer options (lets you customize how the the request is sent and/or returned). Also, it's a good idea to get used to how cURL works generally; see the other PHP reference (client URL library)
try something like this:
$mycontent = file_get_contents('http://SERVER_A.com/mi_inst_installer.php?query-asking-for-missing-item(s)=missin-item');
file_put_contents('missing_installer.php', $mycontent));
you need to get (using file_get_contents()) the contents of the downloaded file first and then put it into the second parameter of file_put_contents() first.

Run a cron job from within a php file

I have a problem where my hosting company won't let me run a cron job in this format from my control panel:
/usr/bin/php /home/sites/MYDOMAIN.com/index.php?option=com_community&task=cron
Or:
www.MYDOMAINNAME.com/index.php?option=com_community&task=cron
Now if i run the second job in a browser i.e.:
www.MYDOMAINNAME.com/index.php?option=com_community&task=cron
this works fine in a browser
My support says I have to create a file to run the URL. The only problem is I don’t know how to run a URL in PHP. I have asked a few sites. But nothing. My file is called bump.php and has the following code:
lynx -dump http://www.MYDOMAIN.com/index.php?option=com_community&task=cron
this is what i have in the file
<?php
echo file_get_contents('DOMAIN.com/index.php?option=com_community&task=cron');
?>
You have to access the file in question via your webserver, not directly by file access. If you access it by file-access, it will just return the php code and not execute it.
There are several options on how to access files via webserver. One is your shown method with file_get_contents. You will need to add http:// in front of the url to tell PHP that you want it accessed remotly and not as a local file.
file_get_contents is not allways configured to allow remotely downloading files. In these cases, it will not work. You can check this link to see the configuration setting for remote accessing files:
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Another solution is to use the curl extension (if available)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
http://www.php.net/manual/en/function.curl-exec.php
There are other extensions if curl is also not available...

Can I do a CURL request to the same server?

I need to implement a way to make POST calls to pages located on the same server or in another server. We cannot use include because the files that we are calling usually call different databases or have functions with the same name.
I've been trying to implement this using curl, and while it works perfectly when calling files from another server, I get absolutely nothing when making a call to the same server where the file is.
EDIT TO ADD SOME CODE:
A simplified version of what I'm doing:
File1.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.myserver.com/File2.php");
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
File2.php
<?php
echo "I'M IN!!";
?>
After calling File1.php, I get nothing, but if File2.php is in another server then I get a result.
Any help?
I tried using both the server URL (http...) and the total address of the files (/home/wwww....)
Be aware that if you're issuing the CURL request to your own site, you're using the default session handler, and the page you're requesting via CURL uses the same session as the page that's generating the request, you'll run into a deadlock situation.
The default session handler locks the session file for the duration of the page request. When you try to request another page using the same session, that subsequent request will hang until the request times out or the session file becomes available. Since you're doing an internal CURL, the script running CURL will hold a lock on the session file, and the CURL request can never complete as the target page can never load the session.
Because when you tried to request to the local server with the public ip, apache couldn't resolve to its local domain. So you have to check which local ip apache is using for that domain. Then you need to edit the /etc/hosts file and add the new row with local ip plus your domain. For example:
My Local ip for that domain in apache's virtual host is : 172.190.1.120 and my domain is mydomain.com
So I will add:
172.190.1.120 mydomain.com
Then your curl will work properly.
You should refactor your code. In addition to what Marc B mentioned, this approach will unnecessarily slow down your script (potentially by a large margin) and cause lots of confusion. No offense, but this is just an incredibly hacky fix for bad logic.

PHP curl post to login to wordpress

I am using php curl to login to wordpress behind-the-scenes as described here:
Wordpress autologin using CURL or fsockopen in PHP
However my script is not setting the cookies necessary to retain the wordpress session. Instead they are being sent back to my script and stored in cookies.txt.
Both the curl script and the wordpress login are on the same server in different directories.
Do I need to write another curl script to manually set the wordpress cookies? Is that possible?
If you're just using the code posted as is then it won't work because the subsequent requests won't send the cookies back on each request. Adding curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); should help (well, at least it will if the cookies actually get saved now - otherwise look into permissions) - well, depending on what your usage scenario is anyway.
You could also check out 10 awesome things to do with cURL for some neat examples on how to use curl (example 4 might just be what you are looking for).
BTW If this script is intended for multiple (concurrent) users, you shouldn't use a static filename, but create a temporary file for each user.
I needed the cookies to be sent to the browser not back to my curl script. The curl script was triggered by a php script running in the browser.
I solved the problem as follows:
Added these params to the curl object:
curl_setopt($ch, CURLOPT_HEADER ,1);
curl_setopt ($ch, CURLOPT_HEADERFUNCTION, 'read_header');
Added a php functions called read_header which parsed out the cookie data
Used setcookie() to manually set the cookies
If anyone wants the details please comment.

PHP Curl get HTTP code, not whole document

I'm using curl in PHP to check the HTTP code when requesting some files, I'm trying to make my speed run faster so I'm wondering is there a way to make it get the HTTP code without actually getting the web page from the remote host
Set CURLOPT_NOBODY to true. This means that rather than preforming a GET or POST request, a HEAD request will be preformed so the remote server will only return the HTTP header.
curl_setopt($ch, CURLOPT_NOBODY, true);
There is also some example code in this answer

Categories