I have an issue with a cUrl. Recently, I have found that the problem is in the syntax of the curl, because when I dump the request I want to execute and put it in PostMan it works, but the function inside my code doesn't and returns 3: 'CURLE_URL_MALFORMAT'. For information my curl version is 7.79.1
I have tried some different approaches with CURLOPT_RETURNTRANSFER, CURLOPT_POST and etc, but it does not work...
Here is my function:
private function cUrl_CreateContactFastFood($merchant_token, $arr)
{
$status = "active";
$Contact_name = $arr["customerName"];
$Contact_email = '';
if($Contact_email=='') {
$Contact_email= 'FastFoodCustomer#ttt.com';
}
$Contact_phone = $arr["phoneNumber"];
$Contact_address = $arr["endAddressResolved"];
$url = 'http://dostavka-bg.com/api_services/insert_contact';
$curl_params = '?keys=f74192da825962d3b1c2b2aa616ab68b&merchant_token='.$merchant_token.'&name='.$Contact_name.'&email='.$Contact_email.'&phone='.$Contact_phone.'&address='.$Contact_address.'&status='.$status;
$ch = curl_init();
$headers = array(
'Accept: */*',
'User-Agent: ',
'Accept-Encoding: gzip, deflate, br',
'Host: dostavka-bg.com',
'Connection: keep-alive'
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url.$curl_params);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
Here is the solved problem:
private function cUrl_CreateContactFastFood($merchant_token, $arr)
{
$status = "active";
$Contact_name = $arr["customerName"];
$Contact_email = '';//$arr["customer"]["invoicing_details"]["company_address"];
if($Contact_email==''){
$Contact_email= 'FastFoodCustomer#ttt.com';
}
$Contact_phone = $arr["phoneNumber"];
$Contact_address = $arr["endAddressResolved"];
$url = 'http://dostavka-bg.com/api_services/insert_contact';
$curl_params = '?keys=f74192da825962d3b1c2b2aa616ab68b&merchant_token='.urlencode($merchant_token).'&name='.urlencode($Contact_name).
'&email='.urlencode($Contact_email).'&phone='.urlencode($Contact_phone).'&address='.urlencode($Contact_address).'&status='.urlencode($status);
$ch = curl_init();
$headers = array(
'Accept: */*',
'User-Agent: ',
'Accept-Encoding: gzip, deflate, br',
'Host: dostavka-bg.com',
'Connection: keep-alive'
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url.$curl_params);
curl_setopt($ch, CURLOPT_POST, 1);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$info = curl_version();
curl_close($ch);
return true;
}
The URL you're passing includes several query string parameters, only some of which are shown in your example, but some of which probably include punctuation or other characters which aren't allowed in URLs. Those need to be correctly encoded by either:
Calling urlencode or rawurlencode on each parameter as you build up the query string.
Using http_build_query to format it all for you, rather than manually concatenating the parts.
Related
I'm trying to get some code of mine to work. but I keep getting the following error. Any thoughts on what's going wrong here? I think I have all the quoatations escaped correctly
{"errors":[{"message":"json body could not be decoded: invalid
character 'L' after object key:value pair"}],"data":null}
I know my query is correct as I can run it in the graphQL playground and get the data.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xxxxxxxxxxxx.com/api/v4/endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"query\":\"{ search(q: \"LM123\") { results { part { mpn manufacturer { name }}}}\"}");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Dnt: 1';
$headers[] = 'Origin: https://xxxxxxxxxxxxx.com';
$headers[] = 'Token: xxxxxxxxxxxxxxxxxxxxxxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
If I run a simple query that doesn't search for a term it works perfectly. Like:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"query\":\"{ categories { name }}\"}");
You have a problem with using double quotes here \"LM123\". When your JSON is parsing, the parser expects, that this \" ends your value and then you will have , \"other_key\": \"...\" in your JSON, but you have LM123... instead.
You can try something like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"query":"{ search(q: \"LM123\") { results { part { mpn manufacturer { name }}}}"}');
So I've been working on a login system with Discord, and this is the first roadblock I've hit.
I'm trying to POST to /oauth2/token/revoke and it keeps giving me back the error "invalid_client".
I've tried using a client secret and not using it, only sending the token, changing the name "access_token" to "token", and a few other things I can't recall.
My code for sending the request is this:
session_start();
//debug thing
echo OAUTH2_CLIENT_ID;
$params = array(
"access_token" => $_SESSION["access_token"],
"client_id" => OAUTH2_CLIENT_ID
);
apiRequest("https://discordapp.com/api/oauth2/token/revoke", $params);
The code for that apiRequest function is adapted from a different thread on here and is as follows:
function apiRequest($url, $post=FALSE, $headers=array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
if($post) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
$headers[] = 'Accept: application/json, application/x-www-form-urlencoded';
if(isset($_SESSION["access_token"]))
$headers[] = 'Authorization: Bearer ' . $_SESSION["access_token"];
// using this to see my headers
var_dump($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
//using this to see the error
echo $response;
exit();
fatalError($code, $_SERVER[REQUEST_URI]);
}
return $response;
}
And yes, both the access token and client ID are valid. They work properly on other requests and I've displayed them on this page and confirmed them to be valid.
Any ideas? Am I missing something stupid?
Worked out the fields you need and fiddled with curl a bit and this code is working.
$params = array(
"token" => $_SESSION["access_token"],
"client_id" => OAUTH2_CLIENT_ID,
"client_secret" => OAUTH2_CLIENT_SECRET
);
$ch = curl_init("https://discord.com/api/oauth2/token/revoke");
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: Bearer ' . $_SESSION["access_token"];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
#error handling
}
curl_close($ch);
with the following code a name with an ID (each name in the gnd can be addressed by an ID) is received via the GND interface. This works.
Now i want to get many names at the same time with a cURL loop. The ID of the URL must always be increased by one and the cURL request must loop. How can I do this?
With this Code i receive for example names from the GND database
<?php
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$url = "http://hub.culturegraph.org/entityfacts/118600001";
$request_headers = [];
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'charset=utf-8';
$request_headers[] = 'Content-Type: application/json; charset=utf-8';
$request_headers[] = 'Accept-Encoding: gzip, deflate, identity';
$request_headers[] = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
for ($i = 1; $i <= 10; $i++)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
curl_close($ch);
$data = json_decode($result, true);
if ($code == 200) {
$data = json_decode($result, true);
echo 'Erfolg';
} else {
$error = $data['Error'];
echo 'Beim anfordern der GND-Daten ist ein Fehler aufgetreten: Fehlercode ' . $error;
echo ' Zurueck<br />';
}
var_dump($data['preferredName']);
Result for URL with ID 118600001 = Gerhardt von Reutern
But how must the code be adapted, so that also the names of the URL's 118600002, 118600003 and so on are output? So as often as it is specified. Be it 100 or 1000 times.
change your $url variable to something like $base_url which is the url without the ID
$base_url = "http://hub.culturegraph.org/entityfacts/";
Then in your for loop you do:
$ch = curl_init($base_url . (118600001 + $i));
this rule can be removed, this is not necessary:
curl_setopt($ch, CURLOPT_URL, $url);
You should also handle the response inside the for loop, else you will only see the last person's name, after an incredibly long load time.
I have tried to call API for send image file using CURL .But I am getting below error:
"statusCode":415,
"error":"Unsupported Media Type"
Please help me.
I have attached code here:
$filename = "screenshot.jpg";
$handle = fopen($filename, "r");
$xml = fread($handle, filesize($filename));
fclose($handle);
$jwt_token = "xxx";
$authorization = "Authorization:".$jwt_token;
$url = "https://prod0-commerce-api.sprinklr.com/media_upload";
$headers = array(
"Content-Type: image/jpg]",
"Cache-Control: no-cache",
"Pragma: no-cache",
$authorization
);
$postdata = array('fileName' => '#'.$filename,
'type' => 'IMAGE'); //<-------------
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url);
//curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 60);
//curl_setopt($soap_do, CURLOPT_TIMEOUT, 60);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $postdata); //<-----------
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($soap_do);
// Check for errors and display the error message
curl_close($soap_do);
$httpcode = curl_getinfo($soap_do, CURLINFO_HTTP_CODE);
echo $httpcode;
echo "<pre>";
var_dump($result);
if($errno = curl_errno($soap_do)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
echo "string";exit();
print_r($result);
The problem is in this block:
$postdata = array('fileName' => '#'.$filename,
'type' => 'IMAGE'); //<-------------
The value of type should be a valid MIME type (aka "media type" or "content type").
A MIME type is an identifier composed of two parts (the type and the subtype) joined by slash (/).
The type identifies the category of content (text, image, audio, video, application etc). The subtype identifies more accurate the content inside the category.
For images, the type is image and there are several subtypes: gif, jpeg, png etc.
A correct MIME type for an image file looks like image/jpeg or image/png and not just IMAGE. This is why the server rejects your query.
The PHP function getimagesize() can be used to find the MIME type of a image stored in a file.
Your code should be like this:
$imgInfo = getimagesize($filename);
$postdata = array('fileName' => '#'.$filename,
'type' => $imgInfo['mime']);
And no, there is no setting in php.ini that writes correct code for you.
I have solved using this solution
<?php
$file = "http://localhost/xxx/screenshot.jpg";
$boundary = md5(time());
$eol = "\r\n";
$params = "----".$boundary.$eol
. "Content-Disposition: form-data;name=\"type\"".$eol
. $eol
. "IMAGE"
. $eol
. "----".$boundary.$eol
. "Content-Disposition: form-data;name=\"file\"; filename=\"screenshot.jpg\"".$eol
. '"Content-Type: image/jpeg\"'.$eol
. $eol
. file_get_contents($file) .$eol
. "----".$boundary."--";
$jwt_token = "xxxx";
$authorization = "Authorization:".$jwt_token;
$first_newline = strpos($params, $eol);
$multipart_boundary = substr($params, 2, $first_newline - 2);
$request_headers = array();
$request_headers[] = $authorization;
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Content-Length: ' . strlen($params);
$request_headers[] = 'Content-Type: multipart/form-data; boundary='. $multipart_boundary;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'xxx');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo "<pre>";
print_r($result);
exit();
Most of time a error 415 appears when you don't set properly the Content-Type. Maybe you can put Content-Type:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpg"));
I am trying to pass a json string into a curlopt_postfield.
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
I have a working hard coded example. However I am having trouble making it dynamic.
$test = "84560";
$data = "{\"id\":\"" .$test. "\"}";
$data2 = "{\"id\":\"84560\"}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $data2);
Why does $data2 work and $data fail?
I have tried json encoding, utf8 encoding, and lots of variations of escaping the quotes all to no avail. What am I missing? I do not get an error simply a NULL response.
Here is the full code. Still not able to get it working. Any additional suggestions as I have tried everything I have found.
$test = "84560";
$data = "{\"id\":\"" .$test. "\"}";
$data2 = "{\"id\":\"84560\"}";
$result2 = getJson("results.json?", $data2);
function getJson($endpoint, $postfields)
{
$URL = "https://app.mobilecause.com/api/v2/reports/". $endpoint;
$ch = curl_init();
$headers = [];
$headers[] = "Authorization: Token token=\"[token goes here]\"";
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch))
{
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);;
return $result;
}
You should simply try:
<?php
$data = array('id' => 84560);
$json = json_encode($data);
echo $json; // Will print { id: 84560 }