Simple HTML DOM Parser - Send post variables - php

I have the Simple HTML DOM Parser for PHP, and I am using the following markup:
$html = file_get_html('http://www.google.com');
However how do I send post variables (like a cURL) to that page and get the response? For example
$html = file_get_html('http://www.google.com', array("Item"=>"Value", "Item2"=>"Value2"));

The documentation doesn't mention it as far as I can see, but after taking a look in the source code I noticed the function you're using accepts a stream context as its third argument. You can create a post request with this PHP feature like this:
$request = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query(array(
'Item' => 'Value',
'Item2' => 'Value2'
)),
)
);
$context = stream_context_create($request);
$html = file_get_html('http://www.google.com', false, $context);
If you don't like contexts or would prefer a different method (like the cURL extension) you could also just fetch the page content using that, then feed it to the parser with str_get_html() or $parser->load(); the class itself does pretty much the same internally with the method you're using right now.

Related

Eventbrite PHP RSVP

I have a list of our Eventbrite events inside our PHP CodeIgniter web app.
Here's the code fo my events list:
<?php
$token = "our_token";
$organizer_id = "our_organizer_id";
$request_url = "https://www.eventbriteapi.com/v3/events/search/?sort_by=date&organizer.id=".$organizer_id."&token=".$token;
$params = array('sort_by' => 'date', 'organizer.id' => $organizer_id, 'token' => $token);
$context = stream_context_create(
array(
'http' => array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n"
)
)
);
$json_data = file_get_contents( $request_url, false, $context );
$response = json_decode($json_data, true);
?>
I used $response inside a loop so I could display them. Here's what it looks like.
Now, I want the users to be able to RSVP inside the system and not by redirecting through Eventbrite. Here's a sample event from the list.
I know it's possible but how can I make it work?
This is my first time to work on Eventbrite and their official documentation is in Python. I already emailed their support if they could provide me a good documentation in PHP but I haven't heard from them.
Although there's an SDK and some libraries available, I don't how how to use them and some of them with examples are deprecated.
Your help is highly appreciated.
This is not possible via Eventbrite's API endpoints. However, you can use Eventbrite's embedded checkout widget to accomplish this.
Here are the Eventbrite Articles on Embedded Checkout:
https://www.eventbrite.com/support/articles/en_US/Multi_Group_How_To/how-to-sell-eventbrite-tickets-on-your-website-through-an-embedded-checkout?lg=en_US
https://www.eventbrite.com/support/articles/en_US/How_To/how-to-add-eventbrite-s-embedded-checkout-to-your-wordpress-org-site?lg=en_US
I hope this helps you!

Guzzle HTML tags in body

I need to send HTML components in the BODY of a POST throught Guzzle.
These are the HTML TAGS:
$strData = "<p style='background:url(/clear.png?org_id=1snn5n9w&session_id=123456789&m=1)'>< / p >
<img src='/clear.png?org_id=1snn5n9w&session_id=123456789&m=2' alt=''>";
My Guzzle code is:
$client = new GuzzleHttp\Client(['base_uri' => 'https://thirdURL.com/Test.htm', 'http_errors'=>true]);
$resH = $client->post('https://thirdURL . com/Test.htm',[
'form_params' =>['body' => $strData]]);
Is this the correct way to send this kind of objects via Guzzle?
Thanks for your help.
Regards.
If you are not trying to send data as a query parameter (think of a form submission) then there is no need to use "form_params".
If all you want is the body to contain the desired html string you should be able to do the following:
$resH = $client->post('https://thirdURL.com/Test.htm',[
'body' => $strData,
]);
More information can be obtained by reading through the Guzzle Documentation on Post Requests
The 'form_params' request option is essentially a short-cut way of performing:
$response = $client->post($uri, [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => http_build_query($form_params_array),
])
So in essence, for your particular use case the body of your http request would contain the post query and the html content.
The answer to your question of 'will it work?', is simply (and albeit unhelpful) -- "I don't know". At the end of the day, it all depends on what the server will accept.

Yii: How to redirect and go to a specific element on page?

I am trying to redirect and go to a specific element on the new page like this:
http://192.168.0.49/x/y/index.php/admin/user/update/id/3#certificate
$this->redirect(array('update', 'id' => $certificate->user_id));
How can this be done?
You can simply create the url without the fragment part and then append it manually:
$url = Yii::app()->createUrl('update', ['id' => $certificate->user_id]);
$url .= "#certificate";
$this->redirect($url);
This code works in a manner that is immediately obvious when reading the code. Apart from that there is also the Yii-specific solution: CUrlManager (the component responsible for building URLs) also recognizes # as a parameter. So you can write:
$url = Yii::app()->createUrl(
'update',
['id' => $certificate->user_id, '#' => 'certificate']
);
That can't be done using redirect.
A work around would be
$url = Yii::app()->createUrl('update', array('id' => $certificate->user_id, '#' => "certificate"));
$this->redirect($url);

Yii php: Displaying a widget in a Tab

i've been using Yii framework for some time now, and i've been really having a good time especially with these widgets that makes the development easier. I'm using Yii bootsrap for my extensions..but i'm having a little trouble understanding how each widget works.
My question is how do i display the widget say a TbDetailView inside a tab?
i basically want to display contents in tab forms..however some of them are in table forms...some are in lists, detailviews etc.
I have this widget :
$this->widget('bootstrap.widgets.TbDetailView',array(
'data'=>$model,
'attributes'=>$attributes1,
));
that i want to put inside a tab
$this->widget('bootstrap.widgets.TbWizard', array(
'tabs' => $tabs,
'type' => 'tabs', // 'tabs' or 'pills'
'options' => array(
'onTabShow' => 'js:function(tab, navigation, index) {
var $total = navigation.find("li").length;
var $current = index+1;
var $percent = ($current/$total) * 100;
$("#wizard-bar > .bar").css({width:$percent+"%"});
}',
),
and my $tabs array is declared like this :
$tabs = array('studydetails' =>
array(
'id'=>'f1study-create-studydetails',
'label' => 'Study Details',
'content' =>//what do i put here?),
...
...);
when i store the widget inside a variable like a $table = $this->widget('boots....);
and use the $table variable for the 'content' parameter i get an error message like:
Object of class TbDetailView could not be converted to string
I don't quite seem to understand how this works...i need help..Thanks :)
You can use a renderPartial() directly in your content, like this:
'content'=>$this->renderPartial('_tabpage1', [] ,true),
Now yii will try to render a file called '_tabpage1.php' which should be in the same folder as the view rendering the wizard. You must return what renderPartial generates instead of rendering it directly, thus set the 3rd parameter to true.
The third parameter that the widget() function takes is used to capture output into a variable like you are trying to do.
from the docs:
public mixed widget(string $className, array $properties=array ( ), boolean $captureOutput=false)
$this->widget('class', array(options), true)
Right now you are capturing the object itself in the variable trying to echo out an object. Echo only works for things that can be cast to a string.

pycurl PostFields option usage

I'm trying to use pycurl to upload a file to Processmaker. app, self.usr, and doc are strings. file is a django file field object. I'm currently just passing the object. I'm fairly sure I'm just passing the incorrect object/type/thing to the ATTACH_FILE field.
The working php POSTFIELDS definition looks like this:
$params = array (
'ATTACH_FILE' => '#/home/test.txt',
'APPLICATION' => $resultCase->caseId,
'INDEX' => 1,
'USR_UID' => $oRandomUser->guid,
'DOC_UID' => '3154812864d55a6e017ff65089604572',
'APP_DOC_TYPE' => 'INPUT',
'TITLE' => "Initial document".date("Y-m-d H:i:s"),
'COMMENT' => "this document was uploaded by the system"
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
And the currently broken python:
c = pycurl.Curl()
data = [
('ATTACH_FILE', (pycurl.READFUNCTION, file.read)),
('APPLICATION', app),
('INDEX' , 1),
('USR_UID', self.usr),
('DOC_UID', doc),
('APP_DOC_TYPE', 'INPUT')
]
post = urllib.urlencode(data)
print post
url = "http://192.168.51.155/sysworkflow/en/green/services/upload"
c.setopt(pycurl.URL, url)
c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, post)
c.perform()
c.close()
Any ideas?
I found a way to solve my own issue. Here is what I did, using poster located here: http://atlee.ca/software/poster/ I did the following:
from poster.streaminghttp import register_openers
import poster
register_openers()
url = "http://192.168.51.155/sysworkflow/en/green/services/upload"
params = {
'APPLICATION' : app,
'INDEX' : 1,
'USR_UID' : self.usr,
'DOC_UID' : doc,
'APP_DOC_TYPE' : 'INPUT',
'TITLE' : 'Test',
'ATTACH_FILE' : open(file.path, "rb")
}
datagen, headers = poster.encode.multipart_encode(params)
request = urllib2.Request(url, datagen, headers)
result = urllib2.urlopen(request)
print result.read()
Much easier to use than pycurl! The problem with my first attempt was that POSTFIELDS can't accept files (without some wrangling) and using an HTTPPOST option would work with the files but was difficult to get working with both file data and field data.

Categories