I'm making a script to be run by a cronjob.
It's suppossed to fetch some json orders and process them.
My script at the moment looks like this:
$json_string = '/admin/orders/7109.json';
$real_url = "https://my-store.myshopify.com{$json_string}";
$user = 'my-user';
$pass = 'my-pass';
$ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = json_decode(curl_exec($ch),true);
curl_close($ch);
file_put_contents(__DIR__ . '/../debug/tracker_test.txt', print_r($result,true));
I'm getting this written into the code file even tho my credentials are correct.
Array
(
[errors] => [API] Invalid API key or access token (unrecognized login or wrong password)
)
Am I missing something ?
Edit: In the private apps section of Shopify it gives an example url format:
https://apikey:password#hostname/admin/resource.json
So now the script looks like this:
$json_url = 'https://my-api-key:my-api-pass#my-store.myshopify.com/admin/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $real_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
but I'm still getting the same error.
Turns out I had a syntax error.
I forgot to update the CURLOPT_URL with the new url variable.
It ended up looking like this:
$json_url = 'https://my-api-key:my-api-pass#my-store.myshopify.com/admin/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
Related
Using Mayan EDMS, I am unable to add a document to a cabinet.
I am using PHP to send, create and upload a document, which works well.
Thereafter, trying to add the document to the cabinet results in the following error
{"detail":"Not found."}
I am at a loss on how to proceed as the API documentation is not clear about the request body and fails on the execute in the swagger documentation.
// Add document to cabinet
$cabinet = $params['cabinet'];
$data = array (
'document' => [$document_id], // values must be a list []
);
$post_data = json_encode($data);
$request_url = 'http://example.com/api/v4/cabinets/' . $cabinet . '/documents/add/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$headers = [];
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($post_data);
$headers[] = 'Authorization: Basic YWRtaW46QndLcFNaQnJURDI3MzN5';
$result = curl_exec($ch);
// $result returns : {"detail":"Not found."}
curl_close($ch);
i wish to convert an html document, which is stored as a string ($html) into PDF using convertAPI via CURL (ie no physical file)
i don't understand how I need to post the $html to the API, i was looking at the example on the convertapi webpage, but i don't seem to be able to make sense of it.
example pasted below.
$html = '<hmtl file contents>' ;
$parameters = array(
'Secret' => 'X?X?X?X?X?X?X',
);
function convert_api($src_format, $dst_format, $files, $parameters) {
$parameters = array_change_key_case($parameters);
$auth_param = array_key_exists('secret', $parameters) ? 'secret='.$parameters['secret'] : 'token='.$parameters['token'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/{$src_format}/to/{$dst_format}?{$auth_param}");
if (is_array($files)) {
foreach ($files as $index=>$file) {
$parameters["files[$index]"] = file_exists($file) ? new CurlFile($file) : $file;
}
} else {
$parameters['file'] = file_exists($files) ? new CurlFile($files) : $files;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
curl_close($curl);
if ($response && $httpcode >= 200 && $httpcode <= 299) {
return json_decode($response);
} else {
throw new Exception($error . $response, $httpcode);
}
}
thank you
Try this short example:
$secret = 'XXXXXXXXXX';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream', 'Accept: application/octet-stream', 'Content-Disposition: attachment; filename="file.html"'));
curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/html/to/pdf?secret=".$secret);
curl_setopt($curl, CURLOPT_POSTFIELDS, '<!doctype html><html lang=en><head><meta charset=utf-8><title>Conversion test</title></head><body>This is html body</body></html>');
$result = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
file_put_contents("result.pdf", $result);
} else {
print("Server returned error:\n".$result."\n");
}
More examples can be found at: https://repl.it/#ConvertAPI
Below is the opencart edit order code .
public function edit() {
$this->load->language('sale/order');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('sale/order');
unset($this->session->data['cookie']);
if ($this->validate()) {
// API
$this->load->model('user/api');
$api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));
if ($api_info) {
$curl = curl_init();
// Set SSL if required
if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
curl_setopt($curl, CURLOPT_PORT, 443);
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));
$json = curl_exec($curl);
if (!$json) {
$this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
} else {
$response = json_decode($json, true);
if (isset($response['cookie'])) {
$this->session->data['cookie'] = $response['cookie'];
}
curl_close($curl);
}
}
}
$this->getForm();
}
if I commented below code . Its showing json exception . But its not showing 500 error . Its showing view part with json exception popup. Continue button is not working . I am using opencart 2x
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));
$json = curl_exec($curl);
Hi I am trying to access the Floodhill Flood Alert API in R using R studio.
http://www.shoothill.com/floodapi/
I'm not entirely sure how I login with the API key I have and then call the API.
I have had success with calling an API using a different API, e.g.
library(jsonlite)
jsondata <- fromJSON("http://api.wunderground.com/api/c86b0e891d592775/geolookup/conditions/q/IA/Cedar_Rapids.json")#access api
names(jsondata)
summary(jsondata)
Help on accessing the Shoothill Flood Alert API would be much appreciated!
This technically isn't a full R answer but the example they give on the API provider page is 100% doable in R with the RCurl package:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$apiKey = '<Your API Key>';
$url = 'https://apifa.shoothill.com/Account/APILogin/';
$postinfo = "apikey=".$apiKey."&persist=false";
$cookie_file_path = dirname(__FILE__) . "cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=1");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
$out = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_URL, "https://apifa.shoothill.com/API/Floods");
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
$html = curl_exec($ch);
curl_close($ch);
echo $html;
return $html;
?>
OmegaHat has a rly detailed explanation of how to use RCurl and you should be able to translate the above pretty well after going through it.
I am logging into Craigslist with CURL to scrape the status of my posted listings. The problem I encounter is the transfer of HTML from CURL $output to file_get_html. While Craigslist statuses are actually nested inside TR elements, I just wanted to test the most basic functions to see if things were getting passed through (i.e. link scraping). They are not.
For example, this doesn't work:
$cookie_file_path = getcwd()."/cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.craigslist.org/login?LoginType=L&step=confirmation&originalURI=%2Flogin&rt=&rp=&inputEmailHandle='.$email.'&inputPassword='.$password.'&submit=Log%20In');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.craigslist.org');
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output;
//
include_once('simple_html_dom.php');
$html = file_get_html($output);
//find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
I know the expression works because it returns links if I put in 'http://google.com', or something or other.
This is how it should be done
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.sitename.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$str = curl_exec($curl);
curl_close($curl);
$html= str_get_html($str);
Shouldn't you be using str_get_html instead of file_get_html?
Since $ouput is a string!