how to test if curl is available in PHP without errors - php

I am trying to detect if curl is installed using PHP in a script run from the command line. I tried the following:
if(#function_exists('curl_version')){
...
}
and
error_reporting(E_ERROR);
ini_set('display_errors', '0');
if(is_callable('curl_init')){
...
}
but in both cases I get this message:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-zts-20121212/curl.so' - /usr/local/lib/php/extensions/no-debug-zts-20121212/curl.so: cannot open shared object file: No such file or directory in Unknown on line 0
I would prefer to hide the error message, but it appears that the # and the error_reporting don't work. Is there a different way to suppress this message?

you could check your installed extionsions
$needed_extensions = array('curl', '... other extionsions to check');
$missing_extensions = array();
foreach ($needed_extensions as $needed_extension) {
if (!extension_loaded($needed_extension)) {
$missing_extensions[] = $needed_extension;
}
}
if (count($missing_extensions) > 0) {
echo 'This software needs the following extensions, please install/enable them: ' . implode(', ', $missing_extensions) . PHP_EOL;
exit(1);
}
'

Related

Compile latex from php: "Error: Invalid XRef stream header"

I have been trying to compile latex from php. I got the help from this tutorial. However, I keep getting this error.
Error: Invalid XRef stream header Error: Invalid XRef stream header pdf.worker.js:232:5
XRef_readXRef#resource://pdf.js/build/pdf.worker.js:3708:13
XRef_parse#resource://pdf.js/build/pdf.worker.js:3296:23
PDFDocument_setup#resource://pdf.js/build/pdf.worker.js:2469:7
PDFDocument_parse#resource://pdf.js/build/pdf.worker.js:2350:7
ensureHelper#resource://pdf.js/build/pdf.worker.js:1971:22
NetworkPdfManager_ensure/<#resource://pdf.js/build/pdf.worker.js:1985:7
NetworkPdfManager_ensure#resource://pdf.js/build/pdf.worker.js:1965:1
BasePdfManager_ensureDoc#resource://pdf.js/build/pdf.worker.js:1832:14
loadDocument/
Note: I have installed texlive in ubuntu so I can use pdftex.
xelatex is also installed.
Web server content:
small latex file contained "latex_template.php" and the
index.php contains the following php script.
ob_start();
include 'latex_template.php';
$outputData .=ob_get_contents();
ob_end_clean();
$texFile = tempnam(sys_get_temp_dir(), 'test');
$base = basename($texFile);
rename($texFile, $texFile.".tex");
$texFile .= ".tex";
file_put_contents($texFile, $outputData);
chdir(dirname(realpath($texFile)));
$console = shell_exec("xelatex {$base}" );
//$console = system("xelatex {$base}" );
header('Content-Type: application/pdf');
$pdf = dirname(realpath($console)).DIRECTORY_SEPARATOR.$base.".pdf";
readfile($pdf);
Note: Manual execution xelatex: log file:

codeigniter file_get_content or curl is not working in our dev server

Needs to get location details for the latitude and longitude,
It just work in other server - functioning correctly, but trying with client dev server getting error, i have tried with this about 4 hours - till didn't get the result. assist would be helpful.
Here is my code:
public function index() {
echo $this->getLocation('39.2323', '-97.3828');
}
function getLocation($lat, $long) {
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" . trim($lat) . "," . trim($long) . "&sensor=false";
$json = #file_get_contents($url);
$data = json_decode($json);
$status = $data->status;
$address = '';
if ($status == "OK") {
$address = $data->results[0]->formatted_address;
}
return $address;
}
Am getting the error like below:
A PHP Error was encountered Severity: Warning Message:
file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=39.2323,-97.3828&sensor=false)
[function.file-get-contents]: failed to open stream: Connection timed
out
Filename: webservice/Test.php Line Number: 17
Backtrace:
File:
/home/colanful/public_html/hoa/application/controllers/webservice/Test.php
Line: 17 Function: file_get_contents
File:
/home/colanful/public_html/hoa/application/controllers/webservice/Test.php
Line: 12 Function: getaddress
File: /home/colanful/public_html/hoa/index.php Line: 292 Function:
require_once
Looks like you need to change an ini setting on the clients dev server.
From #Aillyn's answer:
The setting you are looking for is allow_url_fopen.
You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.
I recommend using cURL over file_get_contents() anyways, since it was built for this.

simple_html_dom.php failed to open stream: No such file or directory

Im trying to use the simple html dom parser within WAMP - for some reason I can't get the php file to recognise the parser - I'm using example code from the parser website and it is not working - the code is as follows;
<?php
include_once('C:\wamp\www\reports\simple_html_dom.php');
function scraping_digg() {
// create HTML DOM
$html = file_get_html('http://digg.com/');
// get news block
foreach($html->find('div.news-summary') as $article) {
// get title
$item['title'] = trim($article->find('h3', 0)->plaintext);
// get details
$item['details'] = trim($article->find('p', 0)->plaintext);
// get intro
$item['diggs'] = trim($article->find('li a strong', 0)->plaintext);
$ret[] = $item;
}
// clean up memory
$html->clear();
unset($html);
return $ret;
}
// -----------------------------------------------------------------------------
// test it!
// "http://digg.com" will check user_agent header...
ini_set('user_agent', 'My-Application/2.5');
$ret = scraping_digg();
foreach($ret as $v) {
echo $v['title'].'<br>';
echo '<ul>';
echo '<li>'.$v['details'].'</li>';
echo '<li>Diggs: '.$v['diggs'].'</li>';
echo '</ul>';
}
?>
So far I have tried having the path as follows;
include_once('C:\wamp\www\reports\simple_html_dom.php');
include_once('http://localhost/reports/simple_html_dom.php');
include_once('simple_html_dom.php');
Here are the error messages I recieve
) Warning: include_once(../../simple_html_dom.php): failed to open stream: No such file or directory in C:\wamp\www\reports\example_scraping_digg.php on line 2
Warning: include_once(): Failed opening '../../simple_html_dom.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\reports\example_scraping_digg.php on line 2
I also get another error on line 6..;
Fatal error: Call to undefined function file_get_html() in C:\wamp\www\reports\example_scraping_digg.php on line 6
Thanks in advance for any help you can offer - It is greatly appreciated.
The file in question is located here : C:\wamp\www\reports\simple_html_dom.php
which is why I'm so confused - Thanks again for your help
After hours of looking at the problem - It was incorrect php.ini settings in the WAMP folder - Thanks

How do I include PHP required libs in an AWS EMR streaming cluster

I've created a PHP project that converts JSON format into AVRO format.
The original project requires PHP libs that I'm not sure how to add on EMR.
This is the stderr log received by EMR:
PHP Warning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in /mnt/var/lib/hadoop/tmp/nm-local-dir/usercache/hadoop/filecache/12/convert-json-to-avro.php on line 3
PHP Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /mnt/var/lib/hadoop/tmp/nm-local- dir/usercache/hadoop/filecache/12/convert-json-to-avro.php on line 3
log4j:WARN No appenders could be found for logger (amazon.emr.metrics.MetricsUtil).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
And here is the main code for the mapper:
#!/usr/bin/php
<?php
require_once 'vendor/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$outputFile = __DIR__ . '/test_avro_out.avr';
$avroJsonSchema = file_get_contents(__DIR__ . '/HttpRequestEvent.avsc');
// Open $file_name for writing, using the given writer's schema
$avroWriter = AvroDataIO::open_file($outputFile, 'w', $avroJsonSchema);
$counter = 1;
while (($buf = fgets(STDIN)) !== false) {
try {
//replace ,null: with ,"null": to prevent map keys which are not strings.
$original = array("null:","userIp");
$replaceWith = array("\"null\":", "userIP");
$data = json_decode(str_replace($original, $replaceWith, $buf), true);
//print_r($buf);
if ($data === false || $data == null ) {
throw new InvalidArgumentException("Unable to parse JSON line");
}
$mapped = map_request_event($data);
var_dump($mapped);
//$avroWriter->append($mapped);
//echo json_encode($mapped), "\n";
} catch (Exception $ex) {
fprintf(STDERR, "Caught exception: %s\n", $ex->getMessage());
fprintf(STDERR, "Line num: %s\n",$counter);
fprintf(STDERR, "buf: %s\n", $buf);
}
$counter++;
}
$avroWriter->close();
Notice I'm using the require_once 'vendor/autoload.php'; which states that autoload.php is under the folder vendor.
What is the right way to load the vendor folder into the EMR cluster (there are needed files there)?
Should the require_once path change?
Thanks.
Following Guy's comment I've used a bash script similar to the one you can find here.
I've changed the require_once 'vendor/autoload.php' line in the code to point to the location where i dropped my files. (/home/hadoop/contents worked perfect).
lastly I've added an EMR bootstrap custom step where you can add the bash script so it can run before the PHP streaming step.

file() [function.file]: URL file-access is disabled in the server configuration error

my code is
<?php
if ($_POST['hiddensms']) {
ini_set("allow_url_fopen", "ON");
ini_set("allow_url_include", "ON");
$smsno = explode(',', $_POST['hiddensms']);
foreach ($smsno as $mono) {
$baseurl = "http://api.xxxxxxxxxxxx.com";
$sql_q = "select firstname,lastname from tbl_newsletter where phone='" . $mono . "'";
$resultcount = mysql_query($sql_q);
$row_total_count = mysql_fetch_array($resultcount);
$first_name = $row_total_count['firstname'];
$last_name = $row_total_count['lastname'];
if ($_POST['sel_name_sms'] == 'FirstName') {
$setname = $first_name;
} else {
$setname = $last_name;
}
$smsBodyText = $_POST['sal_sms'] . ' ' . $setname . '\n';
$text = urlencode($smsBodyText . $_POST['msg_body']);
$to = $mono;
$url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
// do auth call
$ret = file($url);
// explode our response. return string is on first line of the data returned
$sess = explode(":", $ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]); // remove any whitespace
$url = "$baseurl/http/sendmsg?user=xx&password=xxxxx&api_id=3370743&to=$to&text=$text";
//ht$ret = file($url);
$send = explode(":", $ret[0]);
if ($send[0] == "ID") {
echo "success\nmessage ID: " . $send[1];
} else {
echo "send message failed";
}
} else {
echo "Authentication failure: " . $ret[0];
}
}
}
?>
i am trying to send sms by api, but is giving me this error
Warning: file() [function.file]: URL file-access is disabled in the server configuration in /xxxxxxxsubscriber-list.php on line 622
Warning: file(http://api.xxxxx.com/http/auth?user=xx&xx&api_id=xx) [function.file]: failed to open stream: no suitable wrapper could be found in /xxxxxx/subscriber-list.php on line 622
Authentication failure:
i set .htaccess like
<IfModule mod_php5.c>
php_admin_value allow_url_fopen On
php_admin_value allow_url_include On
</IfModule>
but its not working. i dont know what to do, if any idea, please help me
i have searched out on some web they suggest to in php.ini
allow_url_fopen = On
allow_url_include = On
but i can't access php.ini , so any other way to soved this ?
This error is telling you that allow_url_fopen is turned off on your server. It IS possible to config a server to ignore user defined php.ini files they place in their web root. Since they went through this trouble, they probably blocked you from alternative hacks (such as in your htaccess file) too.
allow_url_fopen is disabled by your host for SECURITY reasons! It should be, and should remain, disabled. So stop trying to turn it on. They don't want people running who knows what, getting hacked, and putting the other people on the server at risk.
The only way to get around this is to get a new host, or get a VPS or dedicated server where you have root access.
The alternative to allow_url_fopen would be cURL. You can do the same thing with curl.
cURL Example: http://php.net/manual/en/curl.examples-basic.php

Categories