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);
}
}
Related
Is this possible? We want to achieve this sequence of functions to integrate.I'm kinda new at php curl functions. The first line of code is what we want to achieve.
Here's our php:
<?php
paythrougPaypalSanbox() -> publishSite() -> redirectToCurrentSite()
header("Location:".$dashboard_link);
function paythrougPaypalSanbox() {
$data = $_GET['siteName'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
so on...
curl_close($ch);
}
function publishSite() {
$data = $_GET['siteName'];
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/publish/'.$siteName);
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
so on..
curl_close($ch);
}
function redirectToCurrentSite() {
$data = $_GET['siteName'];
curl_setopt($ch, CURLOPT_URL, 'http://trilogy.editor.multiscreensite.com/home/site/'.$siteName);
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
so on..
}
curl_close($ch);
return $dashboard_link;
}
?>
function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
return false;
}
curl_close($ch);
return $data;
}
if(curl($url)){
// second request
}
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 have the following function:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
I need to have some logic inside this function that if the response code of the URL is for example 404, then return null $data variable. I made an example of what I am looking for:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
if (empty($data) or (HTTP response code is 404)) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch);
$data = null;
return $data;
} else {
// everything is ok
return $data;
}
}
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
if (empty($data) OR (curl_getinfo($ch, CURLINFO_HTTP_CODE == 404))) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch);
$data = null;
return $data;
} else {
// everything is ok
return $data;
}
}
use curl_getinfo($ch) before curl_close() called
I have the following code which works;
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nutritionix.com/v1_1/search");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"appId=id&appKey=key&query=milk");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close ($ch);
print_r($response);
?>
Question is, how do i specify fields to be outputted in the results?
I have tried, "appId=id&appKey=key&query=milk&fields=item_name, nf_calories" but to no avail..
Any help appreciated. Thanks.
Simply adding a question mark in between should do the trick really:
https://api.nutritionix.com/v1_1/search?appId=id&appKey=key&query=milk&fields=item_name
And so on for the field names.
I've generally used this approach when using curl, you can take a look at it if you want:
function curl_get_contents($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
function getUrl()
{
$url = "https://api.nutritionix.com/v1_1/search?appId=id&appKey=key&query=milk&fields=item_name";
return $url;
}
function getData()
{
$i = 0;
$url = getUrl();
$response = curl_get_contents($url);
$data = json_decode($response);
if(!isset($data->data))
{
die("no data.");
}
$data = $data->data;
return $data;
}
EDIT: Try an url like this:
https://api.nutritionix.com/v1_1/search/milk?results=0:20&fields=item_name,brand_name,item_id,nf_calories&appId=APPID&appKey=APPKEY
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.