PHP - Retrieve Facebook image in Google Compute Engine - php

I've tried to retrieve the image data of my Facebook profile picture, using both file_get_contents and curl.
The problem occurs on my Google compute engine instance, while on any other server (localhost - mamp, AWS) the script works fine.
An example for one of the scripts I was using
<?php
var_dump(
json_decode(
file_get_contents("https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720")
)
);
Please keep in mind that I've tried using the parameter redirect=false, and accessing the image url I've got in my json response returned false as-well.
Also, I've tried using wget in SSH to the image's url, which returned (403) Forbidden.
My assumption is that I need to configure something differently in my server, not PHP, but because I'm able to retrieve any other image, with the same script - I'm not sure what.

I've already experienced this exact problem,
Ignoring SSL verification while using cURL did the trick for me.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ignore SSL verification
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720");
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);

Related

API not work on Third Level Domain

i have some problems.
I need to keep data from coinmarketcap. When I was developing on localhost it worked well.
But on third level domain coinfollow.altervista.org i can not receive the data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.coinmarketcap.com/v1/ticker/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
$outputdecoded = json_decode($output, true);
echo $outputdecoded;
I try in another domain mywebsite.com and it worked. I think that the problem is coinfollow.altervista.org domain.
I need to save coinmarketcap data into my database with a simple query.
Does anyone know a solution?
It sounds like there is a server config issue on coinfollow.altervista.org, such as curl not being enabled for php.
You can run phpinfo(); to see if curl is installed.
If it is installed try running echo curl_error($ch) to see if there are any errors returning from curl

Accessing googleapis fails

My user logs into my app, app sends idtoken to my server, server sends request to google, google sends userdata to my server, server puts it in my db. worked fine for 5 Months.
Now:
If I type in my webbrowser
https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= (+ idtoken of my google account extracted from my app)
I'll get the json with my name and so on in milliseconds as it should be.
today, since 6 hours, if my phpfile runs the command
$url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=".$idtoken;
$str =file_get_contents($url);
The page is loading and loading and finally returns no value.
I tried this version to get the .json file
$ch = curl_init($url); // such as http://example.com/example.xml
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$str= curl_exec($ch);
curl_close($ch);
no fix with that.
I cannot install composer because no root access.
I contacted customer support, they told me everything is fine on the serverside.
If after checking on the server's console that you can't curl to it over SSL, but can otherwise, it would suggest the port is blocked. Ask the hosting provider to open it.

PHP curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) too slow

I use this method to get facebook api data. just a search query. but I find use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); cost more time during a curl time (over 10+ seconds).
Is there other curl method can run faster?
NOTE: I am now testing in localhost
$url = "https://graph.facebook.com/search?access_token=".$token."&q=dallas&type=post&scope=publish_stream,offline_access,user_status,read_stream";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
//curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__). '/file.crt'); the way as Lumbendil recommend, download a crt file via firefox. still slowly.
$body= curl_exec($ch);
curl_close ($ch);
PS:I do not want to use a SDK, becuase I failed set SDK in localhost test. Although I have read many articles of how to set in localhost. I have set http://127.0.0.1/facebook as my callback url. But just failed. So I still want to get an easy curl way.
Thanks.
You could use a .crt file and verify against that instead of ignoring SSL verification, as explained here.
To keep all the information in one place: In your code, you should write the following:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/crt/file.crt');
To obtain the certificate, you should go with the browser to the page, and then with "view certificate" you have to export it. Remember that you must export it as X.509 Certificate (PEM) for this to work. For a more detailed guide on how to export the certificate, visit the link provided.
If ignoring to check a certificate takes 10 seconds, the problem is not with the certificate or with the checking and quite frankly, it probably isn't with SSL at all.
Ignoring to check the certificate should be very fast and not be measurable compared to how long the rest of the SSL handshake procedure takes.
To properly track down the problem, I would recommend you use the curl command line tool and its --trace-ascii and --trace-time options to see what seems to take time. You may need to snoop on the network with wireshark or similar to get an even better picture of what's going on.
I can't see how the other suggestions of adding a certificate check to the mix will make anything faster.
Just a side note, but if you do wish to use the SDK you can work around the local issue by editing your hosts file and adding localhost.local for 127.0.0.1. /etc/hosts on a linux machine and C:\WINDOWS\system32\drivers\etc\hosts on a windows machine.
Then in the Facebook app settings, simply set localhost.local as your domain and set your site url accordingly.
You should be ready to go then.

how to capture CURL / XML data with Apache?

I'm developing a gateway script that needs to send info to another provider's server, and I need to debug the code.
Is there a way, on my own Linux + Apache + PHP server to capture the CURL / XML data from this script?
I know with PHP, that I could see for example the $_POST, $_GET or $_REQUEST data in a script, but with CURL I don't actually get to the http://intranet/capture.php script in my browser - so this doesn't work.
Is there any other way, with a script on the server to capture everything that's passed to the server, and dump it to a database / flat file?
I even tried monitoring /var/logs/http/access_log on the Linux server, but it didn't reveal much
So, how can I see what the CURL script does, exactly, as the server sees it?
what you can try is this.
echo htmlentities(file_get_contents('http://intranet/capture.php'));
I'm not sure if this is what you mean but it does the same as curl (sort of)
You want to see the output of curl
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url ); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
$result = curl_exec($ch); // run the whole process
curl_close($ch);
echo htmlentities($result);
I hope this is what you mean
In this case, you are the client and the provider's server is the server.
Assuming you are running the curl command from the client, all you can get to is what Robert Cabri said.
If you are attempting to look at whats being received by the server, you need to have appropriate access and also need to know what application stack the server is running to serve your request.

cURL changing the URL after POST

I am doing an HTTP POST using cURL
$url = "http://localhost:8080/~demo/cgi-bin/execute/100";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
//execute post
$result = curl_exec($ch);
echo("$result");
//close connection
curl_close($ch);
The post gets executed, but the response is shown with the error:
The requested URL /~demo/100 was
not found on this server.
The above URL, obviously, does not exist not the server because (somehow) cURL has changed the URL.
It should have been /~demo/cgi-bin/execute/100 . This URL works in browser.
Please tell me why does it do that?
AND how can i stop this, for what I want?
Install Fiddler.
Enable debugging.
Visit the site in the browser.
Execute php cURL code.
Fiddler will tell you exactly what the web server is receiving and sending. since you are running locally, you can see exactly what php is sending as well. Compare the two and that will tell you the problem.
Maybe cURL tries to access default http port 80? Try to use
curl_setopt($ch, CURLOPT_PORT, 8080)
It may not be cURL that is changing the URL, rather that the web server is sending a redirect header to cURL, pointing at a different location. Perhaps the following would help:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
where is?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Categories