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.
Related
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.
I want to retrieve feeds from superfeedr.com using PubSubHubbub api, but my callback response is not working. I am unable to reach on my callback. Here is my code
class Superfeedr
{
private $topic;
private $callback;
private $hub = 'http://superfeedr.com/hubbub';
public $verbose = false;
function __construct($topic, $callback, $hub='')
{
$this->topic = $topic;
$this->callback = $callback;
if ($hub) {
$this->hub = $hub;
}
}
public function request($mode)
{
$post_data = array (
'hub.mode' => 'retrieve',
'hub.callback' => urlencode($this->callback),
'hub.topic' => urlencode($this->topic)
);
foreach ($post_data as $key=>$value) {
$post_data_string .= $key.'='. $value.'&';
}
$url =$this->hub .'?'.$post_data_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, 'testdata:1234');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch);
}
}
and my php file is
#Retrieve.php
$superfeedr = new Superfeedr('http://feeds.bbci.co.uk/news/world/asia/rss.xml',
'mydomainurl.com/callback',
'https://push.superfeedr.com');
$superfeedr->verbose = true;
$data = $superfeedr->request('list');
Here I want to inform you that my callback url is one of my laravel action. Which is
public function callback(Request $request)
{
\Log::info("Testing before callback");
if(isset($_Get["hub_challenge"])){
echo $_Get["hub_challenge"];
return;
}
// Just for testing
\DB::table('test')->insert(['name' => "Test callback data. Please ignore"]);
}
But nothing happens in my log file and database too. Somebody have any idea then please let me know whats wrong here. Thanks.
New to cURL and trying to load PHP file to instantiate a class with below code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$n = new Test_cURL();
I have cross checked and cURL is enable. Also cross check with that file from remote url is loading by
if (curl_exec($ch) !== FALSE) {
return true;
}
else {
return false;
}
But when I am creating new instance it is giving me error.
Fatal error: Class 'Test_cURL' not found in
So how can I load a file allow to instantiate a class from remote?
UPDATE
All required details including sites URL and filename
<?php
/**
* Remote Class
* URL: http://localhost/php-lib/lib.php
* filename: lib.php
*/
class Test_cURL
{
public $msg;
function __construct($message)
{
$this->msg = $message;
}
public function f_msg($add)
{
return $this->msg . ' is the property and ' . $add . ' is the parameter!';
}
}
/**
* cURL file on other site (could be other server)
* URL: http://test-site/index.php
* Filename: index.php
*/
$url = "http://localhost/php-lib/lib.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
eval($result);
$n = new Test_cURL('loader message');
echo $n->f_msg('method message');
/**
* Error message
*/
Fatal error: Class 'Test_cURL' not found in /var/www/html/test-site/index.php on line 26
You can use file_get_contents then use file_put_contents to store file locally.
include the file and instantiate the class.
Reference: http://blog.kotowicz.net/2010/07/re-hardening-php-how-to-securely.html
Use eval() to run your class and CURLOPT_NOBODY should be false so that it can return string.
Curl code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '$url');
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
eval($result);
$n = new Test_cURL();
echo $n->name;
Class Code:
<?php
echo '
class Test_cURL{
public $name="rasmus lerdorf";
}';
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();
}
}