Facebook Authentication using cURL (php) - php

I'm trying to use cURL to do facebook authentication and here's what I have:
$url = "https://graph.facebook.com/oauth/access_token";
$postString = "?client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$response = curl_exec($curl);
but every time it just returns false.
I'm pretty new to using cURL so I could be making some beginner mistakes, but I'm confused as to why this isn't working at all.
Any help would be greatly appreciated!

This is probably too obvious and it may be a typo, but:
//curl_setopt($curl, CURLOPT_POST, 1);
Should be uncommented.

Remove the "?" from the beginning of your post string.. that should fix it!

Related

Error using curl php, getting http status 500

Hello stackoverflow community, I am having some problems when I try to send a request with curl in php, but something odd is that I have 2 servers, and when I use my code in one of them I get the response Im looking for, but in the other one I get the following response:
Http status code 500
As you can see it says "HTTP Status 500 - empty String", but why is that? in the other server I am not getting that with the same code
Thanks in advice to anyone who can help me out!
Ok mistake by no posting my code, here it is
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl, CURLOPT_POST, 1);
//curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_POSTFIELDS, $string_data);
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);

Consume API with php and cURL

Hi I'm trying to consume an API.
I need to add the API key in the header
API endpoint = http://overheid.io/api/kvk
if you want to test things, you can create an account here: https://overheid.io/auth/register
Documentation is in Dutch and can be found here: https://overheid.io/documentatie/kvk
This is what I came up with but do not get passed authentication.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER => true);
curl_setopt($curl, CURLOPT_HTTPHEADER, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
?>
Edit: I've replaced the initial code part, with the solutions, but still no success.
Can anyone point me in the right direction?
Thanks!
You can try to use the CURLOPT_USERPWD option to pass the Authentication header. You also seemed to have some syntax errors.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERPWD, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
But, this API seem to use a custom header...so you can try the following if it the above does not work:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: yourkey"));
UPDATE:
I used the following with an account I created on their site:
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: key"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
This worked for me when I replaced the "key" part with the 64 character key they provide.

Simple php code in post method which request from web service

I am new to web services. I have tried to request from the example web service at http://www.w3schools.com/webservices/tempconvert.asmx. This is the code I have tried:
<?php
$data = array('Celsius' => '56');
$curl = curl_init('http://www.w3schools.com/webservices/tempconvert.asmx
/CelsiusToFahrenheit');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>
I am not entirely sure, but should the data be a query string
$data = "Celsius=56";
I am sure you are doing this just to test curl. You don't really need a webservice to convert celsius to farenheit
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($curl, CURLOPT_POST,count($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
Use these attibutes. to fetch the data. as some url requires exact parameters.
Note* :- Headers are optional

How to change the HTML source code after calling a curl_init?

I curl_exec a curl_init
I want to make some alterations in the source code of the initialised curl session.(in PHP,JAVASCRIPT or ANYthing else)
Anybody got any idea?
Please help
Try this code out:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://stackoverflow.com/questions/8936870/how-to-change-the-html-source-code-after-calling-a-curl-init");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($curl); // run CURL
curl_close($curl);
echo $html; // and finally, return $html
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
This will return the html code, and after that you can use another function to change this like: str_replace or preg_replace

json_decode limitations?

I am trying to decode the JSON at https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&incslude_rts=0&screen_name=microsoft&count=200&exclude_replies=1&contributor_details=0 with json_decode() in PHP (decoding as an associative array, so the second parameter is set to TRUE.
The problem is that it seems to not do anything (no error, warning either). The data contains 200 tweets + some extra data about them. If I fetch only let's say 50 tweets, the json_decode function runs successfully.
So my question is: is json_decode not able to decode large strings?
Edit: my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // $url is the above mentioned URL
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$content = curl_exec($ch);
print_r(json_decode($content,true));
There is something wrong with your cURL I suppose. I just tried the following code with the URL you have provided and works just fine:
$curl = curl_init();
$url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&incslude_rts=0&screen_name=microsoft&count=200&exclude_replies=1&contributor_details=0';
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$content = curl_exec($curl);
var_dump(json_decode($content, true));

Categories