System Call on PHP
Having a bit of problem with this. Script A calls/includes Script B. Script B has to execute a system call and return to Script A. Fails in flames. When I call 开发者_运维问答Script B on its own, it works just fine, I cannot for the life of me get it to work by calling it on A. Ive already tried
- Including it on A
- Calling it with another system call within A
- Making a bash script that calls B and then calling that bash script with A (I N C E P T I O N)
What are my options here?
Edit for code:
<?php
//B.php
//works fine when called on its own
function readsite ($url)
{
$output=system("curl -ks $url");
return $output;
}
?>
<?php
//A.php
include_once("B.php");
$url="www.google.com";
$read=readsite($url);
echo $read;
?>
I assume that the problem you are trying to solve here is fetching the contents of a website.
Give the following a try:
function readsite($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
The curl extension provides you with a relatively sane and stable way to fetch stuff in php. Look at the docs for more information
This assumes availability of the php-curl extension though.
And of course (as lonesomeday pointed out) you should add the scheme to the url (use 'http://www.google.com' as url).
精彩评论