I wrote this coden which is OK :
$veh['vehicleClass'] = 'Car';
$veh['category'] = 'Van';
$veh['make'] = 'RENAULT';
$veh['model'] = 'Scenic';
$veh['modelDescription'] = 'Turbopoooower';
$veh['firstRegistration'] = "201606";
$veh['mileage'] = "500";
$veh['damageUnrepaired'] = true;
$veh['condition'] = "USED";
$veh['internalNumber'] = "12";
$veh['price']['consumerPriceGross'] = "5400";
$ch = curl_init();
$proxy = PROXY_MOBILE_DE;
$proxy_port = PROXY_PORT_MOBILE_DE;
$loginpassw = LOGINPASSWD_MOBILE_DE;
$url='https://services.mobile.de/seller-api/sellers/1086/ads';
$headers = array();
$headers[] = "Content-Type: application/vnd.de.mobile.api+json";
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vehicle));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
I want to convert this code into cURL command line.
I've tried this :
curl -k -x api.test.sandbox.mobile.de:8080 -basic -u XXX:YYY -d "vehicleClass=Car" -d "category=Van" -d "make=RENAULT" -d "model=Scenic" -d "modelDescription=Turbopoooower" -d "condition=USED" -d "damageUnrepaired=true" -d "firstRegistration=201606" -d "internalNumber=13" -d "mileage=500" -d "price[consumerPriceGross]=5400" -X POST "https://services.mobile.de/seller-api/sellers/1086/ads" -H "Accept: application/vnd.de.mobile.api+json
but this cURL command line doesn't work, maybe something wrong link to parameter price[consumerPriceGross]...
Any idea ?
They API is expecting data in JSON format but that is doing a traditional application/x-www-form-urlencoded POST request.
To get it to work, build a JSON string using some other utility and use that as for the POST data. To prevent curl from encoding the data, pass the -H "Content-type: application/json" header option.
For example:
curl -k -x api.test.sandbox.mobile.de:8080 \
-basic -u XXX:YYY \
-H "Accept: application/vnd.de.mobile.api+json" \
-H "Content-type: application/json" \
-d '{"vehicleClass":"Car","category":"Van","make":"RENAULT","model":"Scenic","modelDescription":"Turbopoooower","firstRegistration":"201606","mileage":"500","damageUnrepaired":true,"condition":"USED","internalNumber":"12","price":{"consumerPriceGross":"5400"}}'
"https://services.mobile.de/seller-api/sellers/1086/ads"
Related
Here is my curl commaad line .
curl -u 'username:password' -X GET -H 'Accept: application/json' -H 'Content-Type: application/xml' -d '<request><layout>1</layout><filtermode>category</filtermode><filtervalue>115399046</filtervalue><limit>1</limit><start></start><sortfield></sortfield><sortdir></sortdir></request>' https://example.com/contacts
I use curl command line. It works for me. Now, I need do it in PHP.
The hard part is: The server accept GET request, but need a xml string in request body. I have tried to change GET to POST, the server did NOT return the correct message.
<?php
$ch = curl_init();
$credentials = "user:pass";
$data = 'xml string';
$page = "/contacts";
$headers = array( "GET ".$page." HTTP/1.0", "Content-type: application/xml", "Accept: application/json", "Authorization: Basic " . base64_encode($credentials) );
curl_setopt($ch, CURLOPT_URL, 'example.com/contacts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($ch);
?>
You can't put the GET line in $header, that causes an invalid header to be sent. The PHP equivalent of -X GET is:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
Here is my curl command:
curl -X GET \
-H "X-Parse-Application-Id: ..." \
-H "X-Parse-REST-API-Key: ..." \
-G \
--data-urlencode 'where={"playerName":"Jonathan Walsh"}' \
--data-urlencode 'count=1' \
--data-urlencode 'limit=0' \
https://api.parse.com/1/classes/GameScore
I am using php.
For the -X, -H I know the equivalent:
curl_setopt($ch, CURLOPT_URL, "https://api.parse.com/1/classes/GameScore");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Parse-Application-Id: ...',
'X-Parse-REST-API-Key: ...',
));
What about the -G, --data flags?
What's the equivalent code?
For posting the data(you can use array though):
$request = 'where={"playerName":"Jonathan Walsh"}&count=1&limit=0';
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$request);
For https:// in your url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
To return the date from curl execution
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
Finally:
$result = curl_exec($ch);
UPDATE:
Didn't check that you are doing GET operation. If you do GET, then it will be simply
$request = 'where={"playerName":"Jonathan Walsh"}&count=1&limit=0';
$url = "http://example.com/" . "?" . $request;
curl_setopt($ch, CURLOPT_URL,$url);
Accordingly to How to switch from POST to GET in PHP CURL, you should use :
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
Recommended read : http://php.net/manual/en/function.curl-setopt.php
Can someone write a PHP script that reproduces the functionality of this linux shell command?
curl -X POST -u "USERNAME:PASS" \
-H "Content-Type: application/json" \
--data '{"aps": {"alert": "this is a message"}}' \
https://mywebsite.com/push/service/
I think I almost got it in my code, but I'm not sure how to handle the --data attribute.
Here's what my code looks like so far:
$headers = array();
$headers[] = "Content-Type: application/json";
$body = '{"aps":{"alert":"this is a message"}}';
$ch = curl_init();
// Set the cURL options
curl_setopt($ch, CURLOPT_URL, "https://mywebsite.com/push/service/");
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME:PASSWORD");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
print_r($result);
example in:
http://code.google.com/apis/gdata/articles/using_cURL.html
curl https://www.google.com/accounts/ClientLogin \
--data-urlencode Email=brad.gushue#example.com --data-urlencode Passwd=new+foundland \
-d accountType=GOOGLE \
-d source=Google-cURL-Example \
-d service=lh2
A general rule: use the "--libcurl example.c" option to get curl to generate source code for a C program that would use libcurl. The API is very similar to the PHP/CURL one as you will see and you should then quickly realize that --data translates to CURLOPT_POSTFIELDS.
Oh, and you'll note that the -X usage is completely superfluous! ;-)
I have the following code :
curl -X PUT -u "<app key>:<secret key>" \
-H "Content-Type: application/json" \
--data '{"alias": "myalias"}' \
https://go.xxx.com/api/device_tokens/<token>/
I tried to convert it to php but it seems not working and i don't know what is the problem.
this is what i tried
<?
$token = $_POST["token"];
$al = $_POST["alias"];
exec('curl -X PUT -u "_rEUqXXXmSVEBXXuMfdtg:vpB2XXXXX_2HZ_XXXX7t-Q" \
-H "Content-Type: application/json" \
--data \'{"alias": "'.$al.'"}\' \
https://go.xxx.com/api/device_tokens/'.$token'/');
?>
$ch = curl_init();
// echo $url; die;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("custom: header"));
$returned = curl_exec($ch);
curl_close ($ch);
There are many more options to do what you want in the PHP docs, don't they help.
I need to know how to convert the code below to .php code (which I'm not good with)
the code:
curl -X POST -u "<application key>:<master secret>" \
-H "Content-Type: application/json" \
--data '{"device_tokens": ["<token>"], "aps": {"alert": "Hello!"}}' \
https://go.xxx.com/api/push/
EDIT: I TRIED this:
<?
$msg = "hello from bob";
$token = "72F75474E3360C4C2F26C6AB16FC1E638FE55FCAC92EE4EB4C196123XXXXXXXX";
exec('curl -X POST -u "_rEUqtOtSmSVEBd8uMfdtg:vpB2wmR8Q_2HZ_Jmi37t-Q" \
-H "Content-Type: application/json" \
--data \'{"aps": {"alert": "'.$msg.'", "sound": "default"}, "device_tokens": ["'.$token.'"]}\' \
https://go.xxx.com/api/push/
');
?>
You can try using PHP's cURL functions to accomplish this. The following should achieve what you want (untested):
$handle = curl_init("https://go.xxx.com/api/push/");
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($handle, CURLOPT_USERPWD, "<application key>:<master secret>");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, '{"device_tokens": ["<token>"], "aps": {"alert": "Hello!"}}')
$result = curl_exec($handle);
curl_close($handle);
Via the cURL library. You should be able to find sufficient information on this specific problem via the documentation on the different curl_* functions in the PHP manual.