PHP: To implement API or WebService to transfer simple data? - php

I'm a bit confused about API vs WebService. But anyway, which one will you follow to.. lets say to provide simple string or xml or json data return.
Currently i have implemented something like:
Server:
echo json_encode("hello, ".$_POST["q"]);
Client:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('q' => 'world!'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
$response = curl_exec($ch);
curl_close($ch);
Normally as the standard way, how do we even call/name it? Shall i name it API? Or WebService?
And in normal way (whenever we need to provide some data out of Server, like above) which one do we implement?

Related

How can i sign in to instagram using php so i can web scrape an instragram url?

I want to web scrape an instagram url so I can obtain the user thats making the post. I know that first to web scrape that url i need to sign in to instagram but the question is that i dont know how to sign in.
$data = array(
"username" => "*******",
"password" => "******"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.instagram.com/accounts/login/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$response = curl_exec($ch);
curl_close($ch);
echo $response;
What you are asking for is against Instagram's terms and conditions:
https://help.instagram.com/1188470931252371
We prohibit crawling, scraping, caching or otherwise accessing any
content on the Service via automated means, including but not limited
to, user profiles and photos (except as may be the result of standard
search engine protocols or technologies used by a search engine with
Instagram's express consent).

PHP - make a phone call using voice modem

Is it possible to make phone call using PHP and voice modem?
PHP will run on local server (XAMPP).
I have found some solutions for asp.net (using 3rd party api and software which is fine IMO), but PHP is required for this project.
You said a 3rd Party is okay, so this might be interesting for you.
Please note that you will need to signup for an account with Sonetel in order to use the api. While this is free, making phone calls cost few cents.
Here's an example code to use their api. The api is initiating two phone calls, one to you and one to the person you are calling. It will first ring the number provided first and if this call is picked up, it will ring the second number and then connects both together.
$url='https://call.sonetel.com';
$data = array('email' => 'abc#xyz.com','password' => 'XXXXXXXXX','call1' => 'XXXXXXXXXX','call2' => 'XXXXXXXXXXXXX');
$data_to_send = http_build_query($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_to_send);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);

mapping automatically Box.com and Php

I am trying to design an Application which can create folder and retrieve that folder from box.com using PHP, I have tried lots of APIs but fail.
Also I want to create the folder with automatic authentication.
I have tried https://developers.box.com/docs/ but can't do automatic authentication.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING,"Content-Type:application/x-www-form-urlencoded");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS,array('client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET_KEY&grant_type=urn:box:oauth2:grant-type:provision&username=MY_EMAIL'));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
here i use my client id, secret key and my email id which are register in box.com
IT Gives Some Kind of error:
{"error":"invalid_client","error_description":"The client credentials
are invalid"}
I would recommend using this BoxPHPAPI. I have used it on a big enterprise app. You have to make sure first that you have a app created at developers.box.com.
Once that is done you can pass all that info to the BoxPHPAPI Class and it should handle your login and authentication.
If you plan on doing many API calls at once or rapid succession I recommended you use curl pooling. It speeds it up quite a bit. From about 4 seconds to under 1 (for 5+ API calls). You can see an example of the pooling in this fork of the BoxPHPAPI.
I hope this helps!

REST-ful connection utilizing cURL header issues

I've spent hours troubleshooting this utilizing PHP documentation, the API documentation, as well as other posts on stackoverflow, and am finally asking for help.
I am attempting to write an interface utilizing the new pbSmartConnections API: API Documentation
I have been having challenges with both fsockopen and cURL, however I seem to be able to get farther in the process utilizing cURL, so that's what I'm presenting here. Here's the challenge:
Per my understanding of the documentation, I should be passing the ApiKey as part of the header. When I do this, regardless of the different ways I've attempted to structure the code, I ALWAYS receive the following response:
{
"ErrorCode": 10,
"Message": "Unauthorized"
}
I'm hoping a fellow SO member can see something in my code below (please offer any criticisms and/or suggestions, too!):
(NOTE: The API key below IS valid. It's connected to an account with nothing of value in it, so feel free to use it in your testing)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>
In case you are wondering, while I would like this to work as part of the header, I HAVE tried passing it as a part of the URL as well:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping?ApiKey=41460b3f-8f35-4878-b78d-49ca7f29c071");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>
The PHP documentation says:
An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
You thus want to use the following line instead of the original:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json", "ApiKey: 41460b3f-8f35-4878-b78d-49ca7f29c071"));
This does not however, solve the problem that the ApiKey probably isn't valid.
I finally heard back from support, and what they indicated was that I was using the incorrect url (although at this time, it IS the url in their API Documentation)
The URL in the API documentation was their STAGING, not PRODUCTION. Amazing what switching the URL to the correct one that they sent in their reply - rest.api.pbsmartconnections.com does for the connection. That one change and everything began working properly.
THANK YOU all who took a look, and to #mvdnes for the recommendations on how to set headers.

Symfony2 sending XML to a remote URL

I would like to send XML data to a remote URL. How to do this in a proper way using Symfony2?
Equivalent in flat PHP using curl would be:
$ch = curl_init("http://website");
$request["queue"] = file_get_contents("file_to_send.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);
Well, from here it looks like a good way to do it. Don't forget, keep it simple.
You might want to wrap it in some class that would manager communication with other servers. This way it would be more testable.

Categories