I am trying to get some information using https://api.facebook.com
The following works just fine: (notice graph.facebook.com)
$q = "https://graph.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
But
$q = "https://api.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
Just does not work. I've tried curl, but I keep getting Method Not Implemented
Following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = "https://api.facebook.com/$params".$sep."access_token=$access_token";
echo $url;
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
echo curl_exec($ch);
echo curl_errno($ch);
curl_close ($ch);
Any ideas?
Such a silly mistake. I post the working example:
The call:
$status = $fb->api("method/fql.query?query=".urlencode("SELECT url FROM url_like WHERE user_id = $ID")."",$access_token);
The class:
class fb
{
public function api($params, $access_token)
{
try
{
$q = "https://api.facebook.com/$params?format=json-strings&access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
if ($response->error->message != null)
throw new Exception($response->error->message);
else
return $response;
}
catch (Exception $e)
{
return ("<b>Error:</b> " . $e->getMessage());
}
}
}
For everyone who don't want to use the facebook php sdk.
Related
I try to request an URL width cURL, but there is no answer and the option CURLOPT_CONNECTTIMEOUT doesn't work.
I have no answer at all... I suppose that the server doesn't answer. I try to use the Chrome plugin called Restlet Client - REST API Testing to test the URL, but no answer too.
private function curlInterrogation(){
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, WS_L2_URL.$this->code);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, WS_USER . ':' . WS_PASS);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, false );
$result = curl_exec($ch);
curl_close($ch);
return $result;
} catch (Exception $ex) {
Noyau::setError('Curl error #%d: %s', $e->getCode(), $e->getMessage() );
return array();
}
}
Each time i run my function, i freeze my browser for the domain (i can browse in other website) and i have to restart my browser.
With the CURLOPT_CONNECTTIMEOUT option, should the execution stop and return that it can not connect to server ?
Thanks for your help
Try to check cURL Php Manual if you're trying to check cURL info.
Nonetheless, just check Php cURL Manual.
Then going to your code (Check my comments):
private function curlInterrogation(){
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, WS_L2_URL.$this->code);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, WS_USER . ':' . WS_PASS);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, false );
$result = curl_exec($ch);
curl_close($ch); //You've just closed the execution early before return
return $result; //Returning just the execution is most likely an empty page
} catch (Exception $ex) {
Noyau::setError('Curl error #%d: %s', $e->getCode(), $e->getMessage() );
return array();
}
}
Try this
function TestCurl(){ //Just function
try{
$base_url = 'https://www.example.com'; //Replace this with your client URL
$user_name = 'user'; //Replace with username
$user_pass = 'pass'; //Replace with password
$ch = curl_init(); //Initializes a new session and return a cURL handle
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $user_name.":".$user_pass);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, false );
curl_exec($ch);
$info = curl_getinfo($ch);
echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
curl_close($ch); // <- Always put this at the end of the execution
}
catch (Exception $e) {
echo $e->getMessage();
}
}
//Call TestCurl function
TestCurl();
I'm trying to get a list of issues using gitlab API. The response is correct testing the URL, but the curl doesnot execute in my php code. I guess it`s because of the multidimensional array. but how do you solve this? Here is my function:
function getissues_list () {
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}
Thanks!
function getissues_list () {
global MAIN_URL; // Is this accessible in this function?
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
var_dump($response);// ADD THIS LINE TO DEBUG!!!!!!!!
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}
My scraping code is not working , I used php cURL , But not returning page content . So please help me , how can I get all content .
function getSslPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$url = "https://www.woolworths.com.au/Shop/SearchProducts?search=Fresh%20Apple";
$ch = getSslPage($url);
echo $ch;
Add this right after your curl_exec() call and copy/paste here the output:
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
i am using codeigniter and my code is as follows :
Controller :
$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $user_addr . '&destination=' . $vendor_city .'&mode=Montreal';
$json = $this->curl_get_contents($json_string);
$obj = json_decode($json, true);
$this->load->view('get_direction_pg',$obj);
public function curl_get_contents($url)
{
$ch = curl_init($url);
if ($ch == FALSE) {
return array('error' =>"failed");
}else{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
return $result;
}
}
and it if i access var_dump($routes);
it says,
undefined variable routes.
try using this: i.e. encoding the vars - before putting into url
$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . urlencode($user_addr) . '&destination=' . urlencode($vendor_city) .'&mode=Montreal';
the parameters should be encoded for url, otherwise it is being invalid, and hence you are getting that error
Try this:
public function curl_get_contents($url)
{
$ch = curl_init($url);
if ($ch == FALSE) {
return array('error' =>"failed");
}
else
{
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
return $result;
}
}
All,
I have written a curl to get the details from Adcash API. Output of this API is to get the token number after login.
Below code is working good but it is not getting the token as output. It is null. Any suggestions.
<?php
try{
Echo "Executing started";
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info) ;
echo $output;
if (FALSE === $output)
throw new Exception(curl_error($ch), curl_errno($ch));
Echo "Executing Completed";
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
I have updated the code to get the subid report. please check and let me know what is the issue.
<?php
try{
Echo "Executing started";
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
$logindata = array (
'login' => 'xxx',
'password' => 'xxx'
);
$logindata1 = http_build_query($logindata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $logindata1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$json_a=json_decode($output,true);
$token= $json_a["token"];
curl_close($ch);
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
$logindata1 = http_build_query($logindata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $logindata1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "token=". $token . "&call=get_publisher_detailed_statistics");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($output);
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
I'm part of Adcash IT team. First of all, thanks for using our API. The problem in your code is that you're using HTTP authentication. Our API is using POST.
This part of your code:
curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
Can be replaced by:
$logindata = array (
'login' => YOUR_LOGIN_HERE,
'password' => YOUR_PASSWORD_HERE
);
$logindata = http_build_query($logindata);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_URL, 'https://www.adcash.com/console/login_proxy.php');
Let me know if it works.