sending queries to remote servers [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to create a php code that will provide all the information that we need in one place.
Some of the information that we need is on https://link
I am able to sending queries directly to it using a php form but the only trouble is it only works after someone has performed a manual search. Once someone has search for something then it works until the browser is closed.
Once a user closes the browser, then it start returning errors again.
try opening this link without doing a manual search. https://link
that would give an error, now open the first link https:// and then search for something.
now open this link again: https://searchType=singleQuery&phrase=exact&keywords=02284065
that should not give an error.
Came someone explain to me whats going on and how I can work around this?
Thanks.
edit:
I do not have access to the code on the remote server. on my end I am currently using
<?php
echo file_get_contents(searchType=singleQuery&phrase=exact&keywords=02284065");
?>
to test out. once I solve this problem then I would develop the code further.
edit: this is what I have so far, this returns the form but does not submit the form.
<?php
$url='https:/';
$keywords ='';
$ch = curl_init() ;
curl_setopt( $ch, CURLOPT_URL, "$url" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$data = array(
'keywords' => "$keywords",
'Search' => 'submit'
);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
$output = curl_exec( $ch );
curl_close( $ch );
echo "$output\n";
?>

It's quite likely that you're trying to use this service in an improper way. These days, many portals just accept queries (and POST requests, to be more precise) from a "whitelist" of clients. A client in this whitelist could be the website itself, or an authorized mobile app.
You should learn something about the Same Origin Policy, as a "basic" of what we're talking about.
Usually, it's up to the site owner deciding who will be able to query the server, and who will not.
Solution: You should contact the healthinfo.moh.gov.on.ca administrators and ask them if they have some APIs, in order to get your data in a more structured and correct way.
EDIT: probably you can do your search after a manual one on the website because of some session data. After that, the session expires and you can't continue.

Agree with Francesco - esp. about the misuse and contacting the provider - but would add, for educational purposes:
Try using a cURL request instead of file_get_contents.
php doc examples
A snippet I happen to be working with right now:
public function triggerMessage(Contact $tocontact, $msgid)
{
$msgurl = 'https://www.server.com/t/?' . $msgid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $msgurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count(get_object_vars($tocontact)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->contactToUrlParams($tocontact));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

Related

I want to autofill the username & password in external URL in PHP in a iFrame

I am stuck it in for 2 days I cant find any solution regarding this problem
The main problem is that I want to autofill the username & password from the when I click on the button
I searched so many solution in Stackoverflow but didn't helped me
I have a button when I click on it and iFrame popup loading the external URL. URL of the site which is loading is "https://bkwins.org/" in it successfully
Now I want to autofill the username & password of the site.
Can this be Acheiveable through the PHP curl methods?
Here is the following code
$url = 'https://bkwins.org/';
$myvars = "Hi";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
No.
To populate the form you need to either:
Run JavaScript on the page the form exists on.
You can't do this from your website due to the same origin policy
A browser extension could do this, but you don't appear to be writing one
Modify the HTML source code of the page
It isn't your site, so you can't do that.
You could fetch the page using PHP/cURL, modify the HTML, then serve the modified HTML to the user from your own site but that wouldn't be coming from the third party site.

Setup Auto-Login/Visit Specific URL? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
There is a website where I want to be logged in always to be seen 7/24 online, because there is a queue among users and you go forward more on this queue if you are more often online. I have a linux server which I am planning to use for this cause but so far I couldn't find anything useful from google. Would be glad if you can help me!
Summary: My server will login for me always to the website and I will be seen online even at night 3 am.
Login and obtain a session with cURL (example code from the Internet):
$username = 'myuser';
$password = 'mypass';
$loginUrl = 'http://www.example.com/login/';
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/protected/download.zip');
//execute the request
$content = curl_exec($ch);
curl_close($ch);
Then you can use this cookie file later on and can just make GET-requests with cURL - for example every half hour. You should have to make a GET-request before logging in probably if tokens are used by the site you are trying to logging in on. (I had same problem with a online-learning system of a university where the professor can see how long you watched the content :-D)

Getting HTML data from php page

I have a URL like this https://facebook.com/5 , I want to get HTML of that page, just like view source.
I tried using file_get_contents but that didn't returned me correct stuff.
Am I missing something ?
Is there any other function that I can utilize ?
If I can't get HTML of that page, what special thing did the developer do while coding the site to avoid this thing ?
Warning for being off topic
But does this task have be done using PHP?
Since this sounds like a task of web-scraping, I think you would gain more use in casperjs.
With this, you can target with precision what you would want to retrieved from the fb-page rather than grabbing the whole content, which I assume as of this writing is generated by multiple requests of content and rendered to you through a virtual DOM.
Please note that I haven't tried retrieving content from facebook, but I've done this with multiple services.
Good luck!
You may want to use curl instead: http://php.net/manual/en/curl.examples.php
Edit:
Here is an example of mine:
$url = 'https://facebook.com/5';
$ssl = true;
$ch = curl_init();
$timeout = 3;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
Note that depending on the websites vhost configuration a slash at the end of the url can make a difference.
Edit: Sorry for the undefined variable.. I copied it out of a helper method i used. Now it should be alright.
Yet another Edit:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
By adding this option you will follow the redirects that are apperently happening in your example. Since you said it was an example I actually didnt run it before. Now I did and it works.

integrating php with bigcommerce api

I am a newbie to the whole concept of the Bigcommerce Api, but my php programming is good.
I am having problems with identifying where to start exactly if i am going to integrate the php code that I may have created with the bigcommerce api,
Where do my files go? How do they interact exactly...
On the Bigcommerce site, they define what its all about, and provide the basic php codes.
Google is my friend but she hasn't been able to help me with links for the whole "How to get started" part...
Or maybe I am asking her the wrong questions.
My ultimate task is to take data from an xml file and use php to handle the rest of the processing and displaying..
I am a newbie and I am not sure how it really works. But links to proper tutorials will really help
Apparently i had no idea Big-commerce doesn't need me to host any server code on their side or whatever,
i found out that which ever code i write is simply run on my side so i can interact with my store.
i wonder why i couldn't really find this information anywhere easily, shouldn't it be the first thing we see when we reach the developers page on bigcommerce.com??
Anyway, i found what i was looking for and i understand how it works now.
To begin, as far as where do your PHP scripts go, those will be hosted external to your Bigcommerce store from any server that has PHP installed or from your local computer. The Bigcommerce API basically gives you a way to access and make changes to your store's database using a program. You will interact with API resources endpoints (URLs) which have been tied to specific store related data, like your products or orders. You can make GET, PUT, POST, and DELETE requests on these URLs in order to pull, modify, create, or delete store related data, respectively.
the php files are here https://github.com/bigcommerce/bigcommerce-api-php ,
and these should be in your machine with a PHP server
to get started there are sample codes there also.
i had problems with this line
require 'vendor/autoload.php';
so i changed it to
require 'path_to_this_file/bigcommerce.php';
everything is working fine now...
but iam still learning more
Have a look at this Link for the list of Resources.
Your php file will be making calls to BigCommerce API, to get data from BigCommerce Store.
Simple cURL snippet to get orders.
$api_url = 'https://YOUR-API-PATH.mybigcommerce.com/api/v2/orders.json';
$ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array ('Accept: application/json', 'Content-Length: 0') );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_USERPWD, "YOUR-USERNAME:YOUR-API-TOKEN" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
$result = json_decode($response);
print_r($result);
Hope this helps.
For that you can not make directly call to your store front..for that make request from third party server to get your store Api.
call bellow php code by Ajax request...
like that..
----------Php Code:-
$username='username';//API User name
$password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//API Token
$URL='https://store-xxxxxxx.mybigcommerce.com/api/v2/products';//API path
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:UTF-8','Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); code
$result=curl_exec ($ch);
curl_close ($ch);
print_r($result);//Your Requested Data

How do you use PHP to duplicate form fields?

I currently have a clients landing page set up so that it posts any new signups to Salesforce. The information primarily goes through to my own system which requires specific form field id's. Salesforce also requires specific form field id's. To do this I have created a second set of form fields that are hidden, and I'm using javascript to set the value of the hidden fields to the same as the visible ones. The only problem with this is that if javascript isn't enabled on the end-users browser, then most of the information doesn't go through to Salesforce.
Is there an alternative way of doing this in PHP?
I'm not an expert with PHP, and my thinking was to post the data to an interrupt page, echo the value of the visible fields in the hidden fields, and then use a header redirect to go to the normal script.
Any suggestions would be greatly appreciated
I wont show you the form as I'm aware you already have one set up
But here is how I would approach this using cURL
$url = 'http://salesforceurl.here';
$post_data = "first_name={$_POST['fname']}&last_name={$_POST['lname']}&email={$_POST['email']}&phone={$_POST['telephone']}&country={$_POST['country']}&description={$_POST['comments']}";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
If you want to avoid having to rely on anything front-end like JavaScript, then cURL is going to be your best bet. With this, you would eliminate the hidden fields. When the user submits the form, your PHP script would gather the information, repackage it for SalesForce, and then POST it to their form script as if entering it directly into their form. This also means that your server is doing the POST rather than the user's browser, so the interaction with SalesForce ends up being completely transparent to the user.

Categories