In my app I'd like to send some small amount of data to my cartodb-table with my php-script.
I would like to use the SQL-API. My app parse the data in the form like:
https://{account}.cartodb.com/api/v2/sql?q=INSERT INTO test_table (column_name, column_name_2, the_geom) VALUES ('this is a string', 11, ST_SetSRID(ST_Point(-110, 43),4326))&api_key={Your API key}
I tried several functions http_get, file_get_contents, http_request but nothing pass the data to my account. Put when I copy/paste the URL and open in the browser, everything is added.
What's the right function? There so many different ones... PHP-HTTP-Functions
EDIT
With the code from this solution I get this error, when I print out the response:
{"error":["syntax error at end of input"]}
within the browser I get this:
{"rows":[],"time":0.038,"fields":{},"total_rows":1}
my request code right now:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullurl);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
print($response);
Found the result: with CURLOPT_POST => 1 it works!
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://".$cartodb_key.".cartodb.com/api/v2/sql",
CURLOPT_USERAGENT => 'Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
api_key => $api_key,
q => "INSERT INTO spot (".implode (", ", array_keys($data)).") VALUES (".implode (", ", $data).")"
)
));
$response = curl_exec($ch);
curl_close($ch);
Related
I am using following code to send SMS in php API.
$ch = curl_init("http://wpsms.whitepearldemo.biz....?user=".$user."&password=".$password."&msisdn=".$msisdn."&sid=".$sid."&msg=".$msg."&fl=".$fl."&gwid=".$gwid);
$result = curl_exec($ch);
curl_close($ch);
Including this response, I also have another response. So it looks like
{
//SMS default response
{"ErrorCode":"000","ErrorMessage":"Success","JobId":"381a80-157cc2142bfa","MessageData":[{"MobileNumber":"919898xxxxxx ","MessageParts":[{"MessageId": "919898xxxxxx -67e3765cdf034f438","MessagePartId":1,"MessageText":"test message"}]}]}
}{
//another response
...
}
When this api called, everything worked properly but app this error -
"onFailer: JSON document was not fully consumed."
.
If I comment SMS code temporarily, no error occurs.
Can we avoid the response of SMS?
Please help me to fix it.
Let me answer my question...Use following code to send SMS without JSON response.
$data = array(
'user' => $user,
'password' => $password,
'msisdn' => $msisdn,
'sid' => $sid,
'gwid' => $gwid,
'fl' => $fl,
'msg' => 'Hi'
);
$curl = curl_init('http://wpsms.whitepearldemo......?format=xml');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
Thanks & regards.
I want to retrieve the final URL formed using a CURL request, but none of them work what I want to retrieve.
for example - I directly paste an API call in the browser address bar, it works, but when using CURL request I get some errors.
To find that I want to retrieve the final request URL formed by CURL or all the request parameters included in CURL Request.
The various methods I have tried is like:
$ch = curl_init();
$f = fopen('request.txt', 'w');
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $f,
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
Alternatively -
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
var_dump($data);
var_dump(curl_getinfo($ch));
Thanks.
Update:
My request should form url like this:
https://api-3t.sandbox.paypal.com/nvp/?METHOD=SetExpressCheckout&VERSION=86&PWD=9mypassword&USER=my_user_name&SIGNATURE=my_api_signature&L_BILLINGTYPE0=RecurringPayments&L_BILLINGAGREEMENTDESCRIPTION0=FitnessMembership&cancelUrl=http://localhost/recurring-payment/index.php&returnUrl=http://localhost/recurring-payment/review.php
Now I want to get all the info in the above URL being submitted to Paypal API.
So I can retrieve this info being sent through CURL.
We're updating our Mailchimp implementation from 1.3 to 3.0. We succesfully updated our code to subscribe someone to a list. Now we're trying to add an ecommerce order. In API v1.3 we did this with the function campaignEcommOrderAdd. I found the function to this with in v3.0: /ecommerce/stores/{store_id}/orders(website link).
But I can't get it to work. When posting to Mailchimp I get an 404 error, but I don't know what I'm doing wrong. Below is my code.
$apiKey = "xxx"; //xxx for privacy reasons
$json = json_encode(array(
'id' => $mailchimp_order['id'],
'customer' => array(
'id' => $mailchimp_order['email_id'],
),
'campaign_id' => $mailchimp_order['campaign_id'],
'currency_code' => "EUR",
'order_total' => $mailchimp_order['total'],
'tax_total' => $mailchimp_order['tax'],
'lines' => $mailchimp_order['items'],
));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/ecommerce/stores/'.$mailchimp_order['store_id'].'/orders';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
This is the output of my $json var:
{
"id":"10000003",
"customer":{
"id":"a90f52f710"
},
"campaign_id":"641657",
"currency_code":"EUR",
"order_total":"56.90",
"tax_total":"47.02",
"lines":[
{
"id":"224",
"product_id":"4427",
"product_title":"Product name",
"product_variant_id":0,
"quantity":"1",
"price":"49.95"
}
]
}
And this is the error I get:
object(stdClass) {
type => 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/'
title => 'Resource Not Found'
status => (int) 404
detail => 'The requested resource could not be found.'
instance => ''
}
Without knowing more I would try a few things here. One (you may have already tried this, but check the output of $url) to make sure that all that is getting set correctly. Secondly I would make sure that the store instance you are posting this order to is reachable/exists by making a GET request to, what would be:
$url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/ecommerce/stores/'.$mailchimp_order['store_id']'
Lastly I would verify that both the campaign and product instances associated with the order are reachable using:
GET https://{dc}.api.mailchimp.com/3.0/campaigns/641657
GET https://{dc}.api.mailchimp.com/3.0/ecommerce/stores/{store_id}/products/4427
Also if you are doing a lot of updating to 3.0 for your app it might be useful to implement a library that abstracts out a lot of this code I use this one:
https://github.com/Jhut89/Mailchimp-API-3.0-PHP
My reputation score wont let me post more links to those endpoints but they should be easily found in the MailChimp documentation. Hope that helps out.
I'm trying to call an API (mapquest) url from a shared host.
The url works fine, as it shows the expected JSON response when pasted in a browser.
However, I can't make it work from a php page.
I tried both curl and file_get_contents with no success.
I keep on getting HTTP/1.1 400 Bad Request error...
Here is the code I use, which is quite basic...
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
if(!$result){
exit ('cURL ERROR: '.curl_error($ch));
}
return var_dump($result);
Hello try some thing like this
$query = urlencode('where={"steps":9243}');
$ch = curl_init('https://api.parse.com/1/classes/Steps?'.$query);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID',
'X-Parse-REST-API-Key: myRestAPIKey',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
refer this link
Querying API through Curl/PHP
I got it!
The url was composed of JSON data with spaces.
This part of the URL must be encoded with urlencode(), but not the full url.
It works both with curl and file_get_contents().
Try below code
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.bing.com/",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'field1' => 'some date',
'field2' => 'some other data',
)
);
curl_setopt_array($ch, $curlConfig);
echo $result = curl_exec($ch);
curl_close($ch);
Im making this API adapter to POST data our OMS (Order Management System). And I keep getting this error. I dunno if it's really an error because the adapter is connected. the POSTing is the problem. I'm using JSON and cURL to pass data to be updated. So here's my code:
$data = array(
'package' => array(
'tracking_number' => '735897086',
'package_status' => 'failed',
'failed_reason' => 'other1',
'update_at' => '2013-11-22 09:58:39'
)
);
and this is how I POST it.
$postdata = "apikey=$apikey&method=$method&data=$check";
$ch = curl_init();
//SSL verification fixed with this two codes
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url.'/webservice/',
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded')
)
);
$result = curl_exec($ch);
and this is my code to test the connection and check if the POSTing is success.
if(curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); } else { echo 'Operation completed without any errors'; }
$result = curl_exec($ch);
echo $result;
curl_close($ch);
I don't really know why I keep getting the "INCORRECT PARAMETERS SENT TO SERVICE". I already reviewed the documentation, the parameters are right. :(
I do believe it is because your POST variables are an array within an array so, what you end up trying to do with your current approach is invalid as stated.
Prior to setting $data in CURL try running the following:
$data = http_build_query($data);
See the PHP definition of http_build_query for more details
I forgot to add this. I encode it to JSON that's why I use arrays.
$check=json_encode($data);
echo $check;
$postdata = "method=$method&data=$check&apikey=$apikey";
$ch = curl_init();
I echo it first before getting the response to check if it's encoded in JSON. then I got this error:
{"package":{"order_number":"200118788","package_number":"200118788-4274","tracking_number":"735897086","package_status":"failed","failed_reason":"other1","update_at":"2013-08-06 17:02:14"}}Operation completed without any errors{"OmsSuccessResponse":false,"message":"Incorrect parameters sent to service","package_status":null}