Fatal error: Call to undefined function http_get() - php

I tried to make cross domain request in php coding using http_get. In my server, there is no required modules installed. I dont know what to install to make posible of doing http_get.
The error which i got was
Fatal error: Call to undefined function http_get() in
C:\xampp\htdocs\article\index.php on line 2
I tried to do so (PECL pecl_http >= 0.1.0)
http_get — Perform GET request
but, i did not find out solution.
So, please help me to run http_get coding. Thanks in advance.

I think that you have to enable extension=php_http.dll in your php.ini file and after that restart your Apache Server.
I advise you to use cURL instead of http_get() (same manipulation, you have to enable extension=php_curl.dll and restart apache)
Hope that Helps :)

To directly answer the question, you've to enable extension=php_http.dll in php.ini and restart apache, but there's absolutely no need to use http_get() in our days.
Here's 3 good alternatives:
The php curl library
file_get_contents()
copy()
Curl usage:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
CURLOPT_USERAGENT => 'User Agent X'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
file_get_content() usage:
$my_var = file_get_contents("https://site/");
copy() usage:
copy("https://site/image.png", "/local/image.png");

Related

file_get_contents() returning empty string

I am having the weirdest, head splitting issue ever. The real issue is I'm getting a SOAP error SOAP-ERROR: Parsing WSDL: Couldn't load from 'some website' : failed to load external entity
In the past it was caused by the host not responding, bad URL, etc. But today I have no problem connecting to the host.
I'm using Symfony to run the API. I stripped ALL of the soap code from the API, created a simple test.php file and ran it from the command line on the exact server the API is running on, and it worked perfectly!
So I went back to the Symfony, flushed the cache. Still not working.
Next I decided to test the simplest methods I could and slapped 'file_get_contents('the url to the WSDL')` at the very top of the service that's making this SOAP call. It returns an empty string. No errors in the error log.
Next I tried moving that to the Controller. Still an empty string.
I tried file_get_contents('https://google.com'). Still, empty string no matter where in the Controller or Service I stick it. But, if on that same server I run a PHP script from the command line, it works no problem.
I don't even know where to begin showing you my code. Because it doesn't matter if file_get_contents() is in the controller, an entity, or the services it's using. Every time it returns an empty string.
As a side note, this is a development server. Which is an exact mirror of production (they just made an image of the production server). The whole API works fine in production and also on my local dev environment and in other environments it's been deployed to. So I'm entirely lost on why it's just this server, and just this particular application.
Any help is appreciated.
==
Update 1
As suggested by a friend I checked allow_url_fopen in both the cli php -i and apache phpinfo() and in both cases it's set to On
==
Update 2
As Pedro suggested I ran a cURL test in the controller of the API. And that worked, it retrieved the contents of the WDSL, and of course Google.
However, it doesn't solve my root problem with SOAP not retrieving the contents of a URL. Nor the obvious file_get_contents() not working. Does the PHP SOAP client use file_get_contents() to retrieve WSDLs?
==
Update 3
For those curious this is where the code is failing. I have to omit a lot of the class because it contains information specific to my company.
<?php
class Carrier implements CarrierInterface
{
//...omitted code
private function getSoapClient($wsdl)
{
// This line is what fails.
return new \SoapClient( $wsdl , array('trace' => true));
}
//...omitted code
public function quote(QuoteInterface $quote)
{
//Get the Soap Client
$client = $this->getSoapClient($this->quoteWSDL);
// The rest of the SOAP CALL
return $quote;
}
}
But in short, $this->quoteWSDL is just a URL, and yes it has a the correct value I've checked. But it fails when trying to construct the SOAP client stating SOAP-ERROR: Parsing WSDL: Couldn't load from '<url>' : failed to load external entity
==
Update 4 (with Kinda Solution)
Restarting apache fixed everything. I'm not sure why I didn't just ask the SysAdmin to do that sooner. That's like IT 101 right there. Thanks for your help. But now I'm curious why this was an issue to begin with. Weird.
My guess is that allow_url_fopen is disabled.
To enable it, you'll have to set allow_url_fopen=1 on php.ini.
Restart the Apache server to reload the configuration.
Have you considered using curl? Here's an example:
$url = "http://google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = curl_exec($ch);
curl_close($ch);
PS: For future users: Make sure you read the comments below.

Error using curl_init() in php environment on BlueMix

I use the PHP runtime on Bluemix. In my solution, I need to invoke the external API available on the Internet on port 3000.
I used curl to manage the API invocation using the following code:
$endPoint="http://".$this->server.":".$this->port.$endPoint;
$session = curl_init($endPoint);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($session);
In the Bluemix log I found the following error:
Got error 'PHP message: PHP Fatal error: Call to undefined function
curl_init() in /home/vcap/app/htdocs/include/service.php on line 50\n',
referer: http://m2msite.mybluemix.net/login.php
It does not seem to be a defined curl library inside my runtime.
I do not found any documentation on how to define the curl library in a Bluemix runtime.
Can anyone help me to configure my runtime in a correct way?
Many thanks in advance
I found the solution.
under the project root I create a directory .bp-config
Inside the directory I create a file option.json with the following content:
{
"PHP_EXTENSIONS": ["mysqli","curl"]
}
mysqli for mysql library and curl for the curl library.
So now I am able to use curl library inside my project
Has anyone the link for a complete documentation around this configuration steps ?
Ciao
the complete documentation for your this kind of configuration related to a PHP buildpack could be found here
https://docs.cloudfoundry.org/buildpacks/php/gsg-php-config.html

Unable to connect to Amazon S3 suddenly

I have been using this PHP class to upload images to S3. Here's an example code:
$s3 = new S3($awsAccessKey,$awsSecretKey);
$s3upload= $s3->putObject(
$s3->inputFIle($tmp_name, false),
$s3BucketName,
$filename,
S3::ACL_PUBLIC_READ
);
Out of nowhere I have been getting this error today:
Warning</b>: S3::putObject(): [7] Failed to connect to bucketName.s3.amazonaws.com port 80: No route to host in <b>/S3.php</b> on line <b>355
I have used the following script to check the curl settings, and I can get the whole google page, so there shouldn't be any problem with CURL
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
Anyone using that PHP class? Have you experienced this problem? How can I find out where the problem lays?
This can happen when some api calls get outdated. I would recommend using the official php libraries. They work almost the same way this library works, but they are being kept up-to-date by Amazon programmers.
I would recommend the official PHP library as well, it's well documented and works well.
No route to host might suggest network problems on your server.
Can you ping bucketName.s3.amazonaws.com from the server?

Fatal error curl_reset() undefined why?

I am using curl to request another site for getting data. My code having curl_reset() funciton is working well at localhost, but when i have updated my code to server. Its giving error
Fatal error: Call to undefined function curl_reset() in
/data/html/reviewkiller1.0/controller/searchController.php on line
2054
Note: I have checked for curl extension it is enabled.
Why it is so???
Requires no PHP version updating.
if (!function_exists('curl_reset'))
{
function curl_reset(&$ch)
{
$ch = curl_init();
}
}
PHP is old on your server.
From the manual: (PHP 5 >= 5.5.0)
My PHP version is 5.3 so i was not able to use curl_reset() function.
Solution
I was using curl_reset() function for getting response of multiple curl request.
So i have removed curl_reset() and used
curl_setopt($curl_handle, CURLOPT_HTTPGET, 1);
curl_setopt($curl_handle, CURLOPT_POST, false);
Problem was after post request my get request was not giving response when i have set curl_post to false to my request it works well.
Conclusion: Its important keep calling setopt to switch between GET and POST request when you are using multiple curl request.
Here is the solution tested 100% working:
Error from php-error-log due to using curl_reset with php version 5.4:
Fatal error: Call to undefined function GuzzleHttp\Handler\curl_reset() in ...
Upgrade to php version 5.5 successfully resolved issue. If you are on cpanel upgrading php version takes only 3 or 4 clicks.
enjoy!
Please mark as answered if it resolve the issue. Thanks!

Internal Server Error 500 simplexml_load_file

if I use Firefox and access http://svcs.ebay.com/services/search/FindingService/v1 I get some sort of XML in respose, when I do that through PHP I get Internal Server Error 500
$ php -r 'print_r(simplexml_load_file("http://svcs.ebay.com/services/search/FindingService/v1"));'
PHP Warning: simplexml_load_file(http://svcs.ebay.com/services/search/FindingService/v1): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
in Command line code on line 1
PHP Stack trace:
PHP 1. {main}() Command line code:0
PHP 2. simplexml_load_file() Command line code:1
PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://svcs.ebay.com/services/search/FindingService/v1" in Command line code on line 1
PHP Stack trace:
PHP 1. {main}() Command line code:0
PHP 2. simplexml_load_file() Command line code:1
$
When I visit http://svcs.ebay.com/services/search/FindingService/v1 in firefox, firebug reports that the HTTP reponse code is indeed 500. (even though it sends some XML in the request body)
You're calling the web service in the wrong way.
Yes, you got XML back, but the response code is 500, meaning your URL is wrong.
Calling ximlexml_load_file via url wrappers expects a success code.
That said, you could probably get at the data anyway. Maybe.
But you should figure out how the service wants you to to query.
If you want to be able to read 500 request data use curl
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://svcs.ebay.com/services/search/FindingService/v1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// grab URL and pass it to the browser
$xml = curl_exec($ch);
$simpleXml = simplexml_load_string($xml);
// close cURL resource, and free up system resources
curl_close($ch);
?>
When I go to that site, I get:
<ms:errorMessage>
−
<error>
<errorId>2038</errorId>
<domain>SOA</domain>
<severity>Error</severity>
<category>System</category>
<message>Missing SOA operation name header</message>
</error>
</ms:errorMessage>
So it would seem that the URL is to a web service and probably requires some kind of authentication or at least input data in the request header. The HTTP response 500, according to Wikipedia, is a generic error meaning that the server can't specify the problem but knows there was one. Here's the best part of that article:
Response status codes beginning with
the digit "5" indicate cases in which
the server is aware that it has
encountered an error or is otherwise
incapable of performing the request.
Except when responding to a HEAD
request, the server should include an
entity containing an explanation of
the error situation, and indicate
whether it is a temporary or permanent
condition.
All that combined, I'd have to say your issue is that your are trying to grab a file from a remote server using a method that assumes you have some directory-level access to that file, and the server is responding with "Um, what?"
If you want to get the actual XML error as though you were in Firefox, use cURL:
$ebay_url = "http://svcs.ebay.com/services/search/FindingService/v1";
$ebay_page = curl_init();
curl_setopt($ebay_page, CURLOPT_RETURNTRANSFER, true); //output to string.
curl_setopt($ebay_page, CURLOPT_URL, $ebay_url); //set the url for the request.
$ebay_response = curl_exec($ebay_page);
print_r(simplexml_load_string($ebay_response));
If you want to actually get something back more meaningful, I would look at PHP's SoapClient methods and the actual ebay web service documentation.
The http headers need to be set specifying the operation to be executed. If you read how to make a call using the finding API, the article mentions the following http headers that need to be set:
X-EBAY-SOA-SERVICE-NAME: FindingService
X-EBAY-SOA-OPERATION-NAME: findItemsAdvanced <-- whatever call you are making
X-EBAY-SOA-SERVICE-VERSION: 1.0.0
X-EBAY-SOA-GLOBAL-ID: EBAY-US
X-EBAY-SOA-SECURITY-APPNAME: MyAppID
X-EBAY-SOA-REQUEST-DATA-FORMAT: XML
X-EBAY-SOA-MESSAGE-PROTOCOL: XML
Once those are set your call shoud work fine.
Maybe you want to try the following URL. When I do, I get back the XML and a 200 response.
http://svcs.ebay.com/services/search/FindingService/v1?wsdl
You need to reinstall your php (assuming you already have apache2 installed). Using these commands:
1) sudo apt update && apt upgrade
2) sudo apt install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php
3) sudo service apache2 restart

Categories