I'm currently trying to send POST data to a URL via curl. The URL that the data is sent to is the URL on which the form is located on. In other words the form "action" is itself.
The problem is, on the form page, a random hidden input value is set on each refresh, and I need to get those values, and use them alongside the other data I want to post. Here is my current code:
<?php
function httpPost($url,$params) {
$postData = '';
$proxy = "127.0.0.1";
$port = "9150";
// Create name value pairs seperated by &
foreach($params as $k => $v) {
$postData .= $k . '='.urlencode($v).'&';
}
rtrim($postData, '&');
$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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_PROXYTYPE, 7 );
curl_setopt ($ch, CURLOPT_PROXY, $proxy.':'.$port );
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
$content = file_get_contents('https://secured.example.com/directory/create');
$params = array(
"name" => "Name",
"pass" => "password",
"email" => "email#email.com"
);
echo httpPost("https://secured.example.com/directory/create",$params);
?>
For instance, on https://secured.example.com/directory/create there is a hidden input field that looks like this:
<input type="hidden" name="anon_token" value="ZHvR0DtwB-15Os0qwdVE3IN_ygSHHhllDAkOUwVPtZE" />
However, the value is randomly set each time (refresh).
After fetching the content, you need to grab the value of anon_token.
This basically uses the content of the HTML page as new DOMDocument.
Then all input tags are fetched, to find the one input tag containing "anon_token".
From this tag you want the value (which is the token).
The token needs to be inserted into the POST data array.
Side-note: a simple preg_match might work, too. But, anyway let's play it safe by using Dom-functions.
$content = file_get_contents('https://secured.example.com/directory/create');
$doc = new DOMDocument();
$doc->loadHTML($content);
$tags = $doc->getElementsByTagName('input');
foreach ($tags as $tag) {
if($tag->getAttribute('name') === 'anon_token') {
$token = $tag->getAttribute('value');
}
}
$params = array(
"name" => "Name",
"pass" => "password",
"email" => "email#email.com",
"anon_token" => $token
);
With preg_match()..
preg_match('/name="anon_token"\svalue="(.*)"/', $content, $matches)
$token = $matches[0];
Hope this works:
$content = file_get_contents("https://secured.example.com/directory/create");
$pattern = "/hidden.*name=[\s|'|\"]anon_token.*value=.*[\"']/";
preg_match( $pattern , $content , $matches);
$pattern_value = "/hidden.*name=[\s|'|\"]anon_token.*value=/";
foreach($matches as $match){
echo preg_replace(array($pattern_value, "/'/" , '/"/') , array( "", "" , "") , $match);
}
Related
I'm trying to get the details from this example (i created the code right now).
But i'm very... confused... how can i get the details of the link, then separate and send to my MYSQL database..
<?php
$ch = curl_init();
$url = "https://reqres.in/api/users?page=2";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
if($e = curl_error($ch)) {
echo $e;
}
else {
$decoded = json_decode($resp, true);
//print_r($decoded);
foreach($decoded as $key => $item) {
$array = array(
'id' => ,
'email' => ,
'first_name' => ,
'last_name' => ,
);
print_r($array);
}
}
curl_close($ch);
?>
If you call the url in your browser then you will see that the result array is present in the data field.
You may check this by printing the whole result:
print_r($decoded);
So if you like to print_r the results it should be simply
print_r($decoded['data']);
If you like to store it in your database you may walk through the array and store each item
foreach($decoded['data'] as $item) {
storeItem($item);
}
To make this work you should implement the storeItem function which accepts the array $item and stores it into your database. There are various tutorials about doing that.
I tried to extract the download url from the webpage.
the code which tried is below
function getbinaryurl ($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
$value1 = curl_exec($curl);
curl_close($curl);
$start = preg_quote('<script type="text/x-component">', '/');
$end = preg_quote('</script>', '/');
$rx = preg_match("/$start(.*?)$end/", $value1, $matches);
var_dump($matches);
}
$url = "https://www.sourcetreeapp.com/download-archives";
getbinaryurl($url);
this way i am getting the tags info not the content inside the script tag. how to get the info inside.
expected result is:
https://product-downloads.atlassian.com/software/sourcetree/ga/Sourcetree_4.0.1_234.zip,
https://product-downloads.atlassian.com/software/sourcetree/windows/ga/SourceTreeSetup-3.3.6.exe,
https://product-downloads.atlassian.com/software/sourcetree/windows/ga/SourcetreeEnterpriseSetup_3.3.6.msi
i am very much new in writing these regular expressions. can any help me pls.
Instead of using regex, using DOMDocument and XPath allows you to have more control of the elements you select.
Although XPath can be difficult (same as regex), this can look more intuitive to some. The code uses //script[#type="text/x-component"][contains(text(), "macURL")] which broken down is
//script = any script node
[#type="text/x-component"] = which has an attribute called type with
the specific value
[contains(text(), "macURL")] = who's text contains the string macURL
The query() method returns a list of matches, so loop over them. The content is JSON, so decode it and output the values...
function getbinaryurl ($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
$value1 = curl_exec($curl);
curl_close($curl);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($value1);
libxml_use_internal_errors(false);
$xp = new DOMXPath($doc);
$srcs = $xp->query('//script[#type="text/x-component"][contains(text(), "macURL")]');
foreach ( $srcs as $src ) {
$content = json_decode( $src->textContent, true);
echo $content['params']['macURL'] . PHP_EOL;
echo $content['params']['windowsURL'] . PHP_EOL;
echo $content['params']['enterpriseURL'] . PHP_EOL;
}
}
$url = "https://www.sourcetreeapp.com/download-archives";
getbinaryurl($url);
which outputs
https://product-downloads.atlassian.com/software/sourcetree/ga/Sourcetree_4.0.1_234.zip
https://product-downloads.atlassian.com/software/sourcetree/windows/ga/SourceTreeSetup-3.3.8.exe
https://product-downloads.atlassian.com/software/sourcetree/windows/ga/SourcetreeEnterpriseSetup_3.3.8.msi
I am submitting a contact form to HubSpot using their api (https://developers.hubspot.com/docs/methods/forms/submit_form), which is working out great, except that every time I do it, HubSpot autogenerates a new form in its site under Marketing -> Forms, with a name like #form_5dd7ee368739f. It says that this is a non-Hubspot form and gives this explanation:
What is a non-HubSpot form
Non-HubSpot forms are HTML forms on your
website that weren't created in HubSpot. Based on your settings,
data for these forms is automatically collected in HubSpot. Learn
more.
"Learn more" isn't a link; I can't click on it. The submission of the api request is recorded both in this new form that it autogenerated each time the form is submitted, as well as in the form that I built in HubSpot that supposed to handle this request. Here is my code:
<?php
// wp-config.php
define('HUBSPOT_PORTAL_ID', getenv('hubspot_portal_id'));
define('HUBSPOT_CONTACT_FORM_GUID', getenv('hubspot_contact_form_guid'));
define('HUBSPOT_CONTACT_FORM_ENDPOINT', "https://forms.hubspot.com/uploads/form/v2/".HUBSPOT_PORTAL_ID."/{form_guid}");
?>
<?php
// hubspot.php
function hubspot_form_submit($page_url, $page_name, $endpoint, $data) {
$hs_context = array(
'ipAddress' => $_SERVER['REMOTE_ADDR'],
'pageUrl' => $page_url,
'pageName' => $page_name,
);
if (array_key_exists('hubspotutk', $_COOKIE)) {
$hs_context['hutk'] = $_COOKIE['hubspotutk'];
}
$data['hs_context'] = $hs_context;
$data_string = "";
foreach ($data as $key => $value) {
if (is_string($value)) {
$value = urlencode($value);
}
else if (is_array($value)) {
$value = json_encode($value);
}
$data_string = $data_string.$key."=".$value."&";
}
$data = rtrim($data_string, "&");
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = #curl_exec($ch);
if(curl_error($ch)) {
$result = curl_error($curl);
}
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
#curl_close($ch);
return $result;
}
?>
<?php
// contact.php
require_once("inc/hubspot.php");
$hubspot_form_submission = hubspot_form_submit(
"https://www.example.com/contact/",
"Contact",
str_replace("{form_guid}", HUBSPOT_CONTACT_FORM_GUID, HUBSPOT_CONTACT_FORM_ENDPOINT),
array(
"firstname" => $form->data["first_name"],
"lastname" => $form->data["last_name"],
"email" => $form->data["email"],
"phone" => $form->data["phone"],
"preferred_contact_method" => $form->data["contact_method"],
"message" => $form->data["comments"],
)
);
?>
Anyone know how I can prevent HubSpot from autogenerating these forms? Otherwise my forms box will quickly become filled up with hundreds of autogenerated forms that I will keep having to delete. Something to note: the actual form that I created for this purpose is located within a folder, whereas the autogenerated forms are always located outside of any folders, if that makes any difference.
Found the problem: the client had enabled the use of Non-HubSpot forms in their settings, so all that is required to fix it is just to turn this functionality off. The documentation is here:
https://knowledge.hubspot.com/forms/use-non-hubspot-forms
The setting is located in
Settings -> Marketing -> Forms -> Non-HubSpot Forms
I've tried to add a thumbnail to the facebook app link, but can't even find documentation about it. Is it possible?
The current code (PHP/Laravel) gives me a working link, which looks like this: https: // fb.me/1234567890. It writes the app name as well when posted on Facebook, but with no image/thumbnail. I've tried putting an "image" or "thumbnail" parameter in http_build_query, but with no luck.
$url = "https://graph.facebook.com/v2.6/app/app_link_hosts";
$ch = curl_init($url);
# create form post data
$metadata = "?item=" . $request->itemid;
$deepLinkURL = "APP://" . $metadata;
//echo $deepLinkURL;
$androidArray = json_encode(array(array("url" => $deepLinkURL,
"package" => "com.app.package",
"app_name" => "APPNAME")
)
);
$iosArray = json_encode(array(array("url" => $deepLinkURL,
"app_store_id" => 45345345,
"app_name" => "APPNAME")
)
);
$webFallbackArray = json_encode(array("should_fallback" => false));
$formQuery = http_build_query(array("access_token" => "1234567890|XXXXXXXXXXXXXXXX",
"name" => "APPNAME",
"android" => $androidArray,
"ios" => $iosArray,
"thumbnail" => "http://i.imgur.com/upnywSR.jpg",
"web" => $webFallbackArray)
);
$path = base_path() . "/vendor/phpunit/phpunit/build/ca.pem";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CAINFO, $path);
# options
curl_setopt($ch, CURLOPT_POST, true); //1
curl_setopt($ch, CURLOPT_POSTFIELDS, $formQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# get response
$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$jsonResponse = json_decode(curl_exec($ch), true);
curl_close($ch);
# decode response from facebook
$appLinkId = "";
# get appLinkId
foreach ($jsonResponse as $key => $val) {
# get status
if($key == "id") {
$appLinkId = $val;
}
}
# if response is good, need to request canonical URL from appLinkId
$errorMessage = "";
$canonicalUrl = "";
if(!empty($appLinkId)) {
# create another instance of cURL to get the appLink object from facebook using the ID generated by the previous post request
$getAppLinkUrl = "https://graph.facebook.com/" . $appLinkId;
$ch2 = curl_init($getAppLinkUrl);
# cURL options
$queryString = http_build_query(array("access_token" => "206722406330430|XRV38UNZsFfRNNF1EkfikzDWkpk",
"fields" => "canonical_url",
"pretty" => true)
);
/////////////////////
$path = base_path() . "/vendor/phpunit/phpunit/build/ca.pem";
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_CAINFO, $path);
/////////////////
curl_setopt($ch2, CURLOPT_URL, $getAppLinkUrl . "?" . $queryString);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
# get response
// $urlResponseJson = curl_exec($ch2);
$urlJsonResponse = json_decode(curl_exec($ch2), true);
curl_close($ch2);
# decode response from facebook
# parse response to get canonical URL
foreach ($urlJsonResponse as $key => $val) {
# get canonical URL
if($key == "canonical_url") {
$canonicalUrl = $val;
}
}
# check for result
if(empty($canonicalUrl)) {
$errorMessage = "Unable to retreive URL.";
}
} else {
$errorMessage = "Unable to publish appLink.";
}
# encode response back to your app
if(empty($errorMessage)) {
$response = json_encode(array("result" => "success",
"canonical_url" => $canonicalUrl));
} else {
$response = json_encode(array("result" => "failed",
"errorMessage" => $errorMessage));
}
return $response;
I've tried to add a thumbnail to the facebook app link, but can't even find documentation about it. Is it possible?
No.
As https://developers.facebook.com/docs/applinks/hosting-api says,
If your application doesn't have a website for content you want to share to Facebook, you don't have public web URLs which you can annotate to support App Links. For these types of apps, Facebook provides an App Links Hosting API that will host App Links for you.
So if you have public web URLs that you want to share, then you should rather annotate those with the meta tags for App Links – then it will take the thumbnail you specified for those URLs via og:image.
If that is not an option, then you could still try and specify a thumbnail when you share the canonical URL of the App Link object, f.e. via the Feed dialog.
I want to extract a "name" and an "ID" from a table which is loaded via a url. the table always contain different values.
One table could be this one: https://www.lectio.dk/lectio/262/FindSkema.aspx?type=stamklasse
i want to take every name and the id in the link its pointing to.
ex: name: 1m/id: 4514546468 (id from link: https://www.lectio.dk/lectio/262/SkemaNy.aspx?type=stamklasse&klasseid=4514546468)
how do i filter everything away but the things i need ? i get the source code with cURL and want to filter away the code so i have what i need left.
i did this whit a list with always fixed values, but since the table is always with different values i cant use the same code:
<?php
$ch = curl_init("http://www.lectio.dk/lectio/login_list.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
$output = curl_exec($ch); // do it!
curl_close($ch); // close handle
$expld = explode("<a", $output);
$list = array();
$remove = array("/", "d");
$removefrom = array("/", "'", ">", "<");
foreach($expld as $key){
$gymnr = substr($key, 15, 3);
$gymnr = str_replace($remove, "", $gymnr);
$gymname = substr($key, 54);
$gymname = str_replace($removefrom, "", $gymname);
$gymname = substr($gymname, 0, -40);
$prelist = array($gymname, $gymnr);
$gymlist[] = $prelist;
};
unset($gymlist[0]);
foreach($gymlist as $school){
echo "<li data-id='".$school[1]."'>".$school[0]."</li>";
}
?>
I think i can use regex to filter this, but i don't know enough about it, i need examples or solutions.