There is a fatal error when I use curl from Laravel but a curl that exists in another file is working.
Why do these errors occur?
fatal error: call to undefined function App\Http\Controllers\curl_init()
App\Http\Controller\FcmController:
class FcmController extends Controller
{
public function index()
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
dd($result);
curl_close($ch);
App\Http\Controller\CurlContoller (It works):
class CurlController extends Controller
{
public static function getCurl($url, $params)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/json; charset=utf-8"));
$response = curl_exec($ch);
curl_close($ch);
return json_encode($response);
}
Does it work if you prefix the function to a top level namespace \curl_init. For the statically called function inside CurlController it won't be needed.
Also, not specifically answering your question but if you are running Laravel 7 and above you could actually use the built in http-client implementation from Laravel. It is more expressive than curl.
Related
I am using Oracle RightNow which uses the Zend framework. I have the code below in a model.
function getTicketAvailability($id){
\load_curl();
$url = "https://www.eventbriteapi.com/v3/events/".$id."/ticket_classes/?token=XXXXXXXXXXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$body = \curl_exec($ch);
curl_close($ch);
$json = \json_decode($body,true);
return $json["ticket_classes"][0]["on_sale_status"];
}
If I call it once in a page, all is well. If I have to call it twice, I get the following:
Function registration failed - duplicate name - curl_init
I also get the same for curl_copy_handle, curl_version, curl_setopt, curl_setopt_array etc
Any ideas on how to resolve this issue?
As it seems load_curl initializes those functions, so check first if they are existing, if not load it.
if (!function_exists("\curl_init"))
{
\load_curl();
}
namespace Service\Component\ThreadWebRequest;
use Thread;
class ThreadWebRequest extends Thread {
public $url;
public $headers;
public $data;
function __construct($url, $headers) {
$this->url = $url;
$this->headers = $headers;
//$this->start();
}
public function run() {
$timeout = 0; // set to zero for no timeout
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0"); //Optional
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Avoid SSL certificate issue
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
$file_contents = curl_exec($ch);
if(curl_errno($ch))
{
return 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
$this->data = $file_contents;
}
}
In my controller I have this,
$threadRequest = new ThreadWebRequest($value['uri'], $headers);
$threadRequest->start() && $threadRequest->join();
$batchResponse = $threadRequest->data;
I am getting this error.
PHP Fatal error: Call to a member function findFile() on a non-object in /opt/services-api/v2/vendor/composer/ClassLoader.php on line 300
If I access thread outside slim it's working fine. Not sure what needs be done to start a thread within slim framework router.
because my server host disabled the use of
file_get_contents()
inside simple_html_dom.php,
I replaced
file_get_html()
with
function file_get_html_using_CuRL($url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
But the code below
$html = file_get_html_using_CuRL($url);
$arvl2_arr = $html->find('div[class="arvl2"]');
returns error:
Fatal error: Call to a member function find() on a non-object in
I am guessing that the problem is because $html isn't object?
The code worked when i used
$html = file_get_html($url);
Is there any way to solve this issue?
There's str_get_html() function available in SimpleHTMLDOM that you can use in order to load up the curl return values.
Just modofy it accordingly:
function file_get_html_using_CuRL($url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$output = str_get_html($output);
curl_close($ch);
return $output;
}
This returns a SimpleHTMLDOM object in which you can now chain up methods of your liking like ->find(), etc. then make your necessary logic.
Note: Of course load up the SimpleHTMLDOM library first.
I have some code of which only works on one post, however i want it to work on all posts not just the one that has been listed i get the following error:
(Fatal error: Cannot redeclare class shareCount in counter/share-count.php on line 3)
loop.php (this is from my loop code for per post)
<?
require("counter/share-count.php");
$obj=new shareCount("http://google.com");
echo "Tweets: ".$obj->get_tweets();
echo "<br>Facebook: ".$obj->get_fb();
echo "<br>Google+: ".$obj->get_plusones();
?>
share-count.php (this is the file thats executable on request)
<?
class shareCount {
private $url,$timeout;
function __construct($url,$timeout=10) {
$this->url=rawurlencode($url);
$this->timeout=$timeout;
}
function get_tweets() {
$json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
function get_fb() {
$json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
$json = json_decode($json_string, true);
return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}
function get_plusones() {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($this->url).'","source":"widget","userId":"#viewer","groupId":"#self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($curl);
curl_close ($curl);
$json = json_decode($curl_results, true);
return isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['count'] ):0;
}
private function file_get_contents_curl($url){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
}
?>
Your included file counter/share-count.php defines the shareCount class every time it is included, which is why you are seeing the error. You can fix this by either using require_once() or check to see if the class is already defined using class_exists() and only defining it if the result is false.
Using require_once() to replace require():
require_once("counter/share-count.php");
// ... rest of your code
Using class_exists():
if ( ! class_exists( 'shareCount' ) ):
class shareCount{
// your class implementation
}
endif; // class_exists
im trying to have a good practice at abstract class Methods so i created a simple curl wrapper class but unfortunately it doesn't work .
abstract
<?php
abstract class curl{
private $url;
public function __construct($url){
$this->url = $url ;
}
public function curl_grab_page()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
ob_start(); // prevent any output
return curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
}
public abstract function getHTML();
}
?>
child
<?php
class google extends curl{
private $url;
function __construct($url) {
parent::__construct($url);
}
function curl_grab_page(){
parent::curl_grab_page();
}
function getHTML(){
return $this->curl_grab_page();
}
}
and this is how i call in my front page .
<?php
include 'classes/class.curl.php';
include 'classes/class.google.php';
$google = new google('http://www.google.com/');
echo $google->getHTML();
?>
it didn't print out anything .
i tried the function separately and it goes fine
Looks like you are not localizing the results of the output buffer before calling return, try this:
public function curl_grab_page()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// Don't need any output buffering b/c you set CURLOPT_RETURNTRANSFER to true, which means the results will be returned via curl_exec
//ob_start(); // prevent any output
//return curl_exec ($ch); // execute the curl command
//ob_end_clean(); // stop preventing output
$contents = curl_exec($ch);
curl_close ($ch);
return $contents;
}
And the child class:
<?php
class google extends curl{
// Don't need this, if you set the parent's scope to protected which means child class has access
// private $url;
function __construct($url) {
parent::__construct($url);
}
// Don't really need this method unless you plan on doing some custom logic
function curl_grab_page(){
// If you keep this method I added a return here so we can get the results of the call
return parent::curl_grab_page();
}
function getHTML(){
return $this->curl_grab_page();
}
}