Setting up cURL with Facebook's linter - php

I'm new to programming and I don't know how to set up a normal PHP extension like cURL. I've installed PEAR packages before but that's all. I think what I'm trying to do is very simple - just getting Facebook's linter to lint my URL upon a page reload on my site. The code Facebook suggests is simply this:
curl https://developers.facebook.com/tools/lint/?url={YOUR_URL}&format=json
Is this supposed to just work if I throw it inside of <?php ?> tags, or is Facebook not assuming that I use PHP? Let's say my site's URL is http://www.example.com - how should this code look in a PHP file? And how am I supposed to install the cURL library? Sorry for being clueless! ;)

This should help you installing cURL: How to install PHP/CURL?
You can use a code similar to this to get the page:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://developers.facebook.com/tools/lint/?url=" . urlencode ( 'http://www.example.com/' ) . "&format=json");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>

Related

Parsing Redirect Response Codes From Redirect URL - PHP

Thank you for your time.
My purpose for posting this question was to see how I can approach the problem of parsing the authorization code I get from the redirect to use that code in a subsequent function to get the oauth bearer token.
What libraries can I use similar to python's request module and web module to achieve the same goal as the following code?
class ILToken(object):
def GET(self):
form = web.input(code=None, scope=None)
As an outcome, here is the following example. I have a redirect url set to "http://redirect_example.com/oauth/". When I click a button, the instant login link ("https://api.provider.com/oauth/authorize?response_type=code&redirect_uri=http://redirect_example.com/oauth/...") redirects me to "http://redirect_example.com/oauth/?code=ExampleAuthCode".
I've tried making use of the curl library in php with no positive progress using code similar to the following.
Code
# Initialize curl request parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, env('INSTANT_LOGIN_URI'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
# Set variables and execute
$output = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$redirect = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
curl_close($ch);
# Print Results
print $redirect;
Related articles say to set FOLLOWLOCATION to true and use the CURLINFO_EFFEFCTIVE_URL variable to return the code. This did not work for me. I am using php v7.4 on an ubuntu machine. The php app is using the laravel framework and loading behind an nginx v1.21 web server.
How can I get the destination URL using cURL?
PHP CURL redirect

cURL request getting wrong request version in Google App Engine

I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!

How to get GitHub raw file contents with PHP?

I am browsing Github info/docs but cant find any simple example on how to
get contents of a raw Github file.
For example if I try to use
$url ="https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html";
echo file_get_contents($url);
I get failed to open stream: HTTP...
If I use curl same thing 404 page. So obviously I have to use API but there is just no simple example on how to do it.
Can someone please post plain example.
Thank you!
You can use a different method to escape SSL validation and add more options if you want, with CURL function.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
Works fine for me here, you just need to enable, if was not, the curl extension.

Google feed api not returning results with php in wordpress

i am trying to use the google feed api in my wordpress site. i have enabled php with a plugin, which allows me to input php code in my pages. my hosting provider also confirmed they have curl enabled.
This is the code which iam trying to run which i got from the google developer site
(https://developers.google.com/feed/v1/jsondevguide#basic_query)
<?php
$url = "https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=iphone5& userip=2.96.214.41";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, http://www.iweb21.com);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
?>
i don't get any results, just a blank page.
i am not a php programmer. just a novice wordpress user. i have been searching for a plugin to use the google feed api but got nowhere. so i decided to try using the code provided by google.
i Would very much appreciate any advise. thnx
Blank page means that there is a Fatal or Parse error, and error reporting is disabled in PHP settings. See this: How to get useful error messages in PHP?
In your particular case, the referer string is not enclosed in quotes and generates a Parse Error. Replace with:
curl_setopt($ch, CURLOPT_REFERER, 'http://www.iweb21.com');

Call to new HttpRequest fails

I am trying to get a PHP script working. The purpose of the script is to call out to a web service. I've reduced the script down to it's simpliest components and it is still failing. Here it is:
<?php
print "Hello";
$request = new HttpRequest('http://www.pivotaltracker.com/services/v3/source_commits', HttpRequest::METH_POST);
print "Done";
?>
The output is:
D:\svn\svndb\hooks>"c:\Program Files\PHP\php.exe" -f test.php
Hello
D:\svn\svndb\hooks>
As you can see, the script fails when trying to instantiate an instance of HttpRequest. However, no exception is thrown.
I am not a PHP program... I'm just trying to get this feature working. I suspect I have no loaded an extension library that I need... but I can't figure out which one that would be, if indeed that is the problem.
I am running on Windows 2003. I am running PHP 5.3.3.
I did run phpinfo() but am hesitant to post the results here since it is so large. Is there a section of the phpinfo() output that would be helpful to provide?
Put a error_reporting(E_ALL); in front and see what happens.
My bet is that the HTTPRequest class doesn't exist. The HTTP extension is a PECL package that needs to be installed separately.
Thank you everyone for your answers. They were all spot on. I thought I'd summnarize what I did in the end in case it helps someone else.
The problem was indeed that I had not installed the http PECL extension. Unfortunately, I am on windows and there was no distriubtion of this extension and I didn't want to install the microsoft tools on this box to be able to compile the source. So, I went with the suggestion listed above and implemented it using curl.
The script I was working on was to integration svn to http://www.pivotaltracker.com using the excellent php script found at http://phpjack.com/content/pivotal-tracker-and-subversion. I modified that script as follows (in case someone else is in a similar spot):
$request = new HttpRequest('http://www.pivotaltracker.com/services/v3/source_commits', HttpRequest::METH_POST);
$headers = array(
'X-TrackerToken' => $token,
'Content-type' => 'application/xml'
);
$request->setHeaders($headers);
$request->setBody("<source_commit><message>$message</message><author>$author</author><commit_id>$rev</commit_id></source_commit>");
$request->send();
became
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml","X-TrackerToken: $token"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close ($ch);
print $result;
Thanks again for all the excellent and timely advise.
Error reporting by error_reporting( E_ALL );
Enable display errors ini_set('display_errors', 1);
Better to change these settings from php.ini.
If it's not working look at apache logs (error.log)
You could use cURL for that simple purpose:
<?php
$url = "http://www.pivotaltracker.com/services/v3/source_commits";
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL, $url);
// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);
// parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, "someParameter=someValue");
$result = curl_exec ($ch);
curl_close ($ch);
print $result;
?>
Or use fsockopen() to connect to a server and fwrite to send a raw http post request.

Categories