Bitbucket API bad request while trying to merge - php

This is my code:
$url = "https://bitbucket.org/api/2.0/repositories/***/***/pullrequests/35/merge";
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl1, CURLOPT_USERPWD, "***:***");
curl_setopt($curl1, CURLOPT_HEADER, true);
curl_setopt($curl1, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_POST, true);
echo curl_exec($curl1);
Thats the response:
HTTP/1.1 400 BAD REQUEST Server: nginx/1.5.10 Date: Wed, 04 Mar 2015 06:03:15 GMT Content-Type: text/plain Content-Length: 11 Connection: keep-alive X-Served-By: app19 X-Render-Time: 0.0410010814667 Content-Language: de X-Static-Version: 572a80470390 Vary: Authorization, Accept-Language, Cookie X-Version: 1d224fb664b6 ETag: "825644f747baab2c00e420dbbc39e4b3" X-Request-Count: 27 X-Frame-Options: SAMEORIGIN Bad Request
Why does this not work? (For safety reasons i replaced some informations with ***)

You are missing the mandatory parameters for that particular endpoint which should be included in your request body.
According to the API, those mandatory parameters are owner, repo_slug and pull_request_id.
$request_body = array(
'owner' => 'account-name',
'repo_slug' => 'repo-name',
'pull_request_id' => 35
);
Because you specified application/json as your Content-Type, you need to json_encode the array from above:
curl_setopt($curl1, CURLOPT_POSTFIELDS, json_encode($request_body));
As a side note, you could use bitbucket-api library, which can help you to interact with Bitbucket API in a more easy way.
Accepting a pull request using that library, looks something like this:
$pull = new Bitbucket\API\Repositories\PullRequests();
// set your login credentials here
$pull->getClient()->addListener(
new \Bitbucket\API\Http\Listener\BasicAuthListener('username', 'password')
);
$pull->accept($account_name, $repo_slug, 35);
You can read more in the docs.
Disclaimer: I am the author of bitbucket-api library.

Related

"WISO mein Büro" API doesnt work or it is my fault

unfortunately I get no good response from the api provider.
No I need your support. I build a test extension in "wiso mein büro" for testing.
I would like to create an order with php and this api:
https://api.meinbuero.de/openapi/documentation/#/orders/postorderCreate
My code:
$wisoApiKey = "XXX";
$wisoSecretApiKey = "XXX";
$wisoOwnershipId = "XXX";
$positionen[] = array ('id' => 769541, 'amount' => 60);
$ch = curl_init('https://api.meinbuero.de/openapi/order/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json" , "Authorization: Bearer ".$wisoToken));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $wisoApiKey . ":" . $wisoSecretApiKey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
array (
"customerId" => 1698487,
"articles" => $positionen
)
));
$createOrder = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($createOrder);
Result:
HTTP/1.1 500 Internal Server Error
Server: nginx/1.19.0
Date: Mon, 09 Aug 2021 18:38:49 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=7776000; includeSubDomains; preload
Access-Control-Allow-Origin: *
All other apis works fine like create, get customer, create invoice from order, ...
Only the api create order doesn't work for me.
I don't know why ...

Using API of battlefieldtracker.com

I'm trying to use the API of battlefieldtracker.com.
There is a docu under: http://docs.trnbattlefield.apiary.io
At first I generated an API-key. My second step was to use the documentation to generate the request:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://battlefieldtracker.com/bf1/api/Stats/DetailedStats?platform=3&displayName=PENTA-piidde&game=tunguska");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"TRN-Api-Key: xxxxxx-yyyyyy-aaaaaa-bbbbb-ccccc"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
The result in PHP is:
bool(false)
In the online try-area of the documentation is my result in any case:
501
Headers
server:Cowboy
connection:keep-alive
x-newrelic-app-data:PxQDVFVRCQITVlZRDgcFV0YdFGQHBDcQUQxLA1tMXV1dSnwZQRNWERdcRE4hJmwcH05DThoBGVZUABoDTFZVWgBQAVsIChgCH0cIVAdUClQAUlMCVVZbBgBGTQRWXUQDOQ==
access-control-allow-origin:*
access-control-allow-methods:OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
access-control-max-age:10
x-apiary-transaction-id:5908b3c55e9e851900cf77f1
content-length:0
date:Tue, 02 May 2017 16:28:53 GMT
via:1.1 vegur
Does anyone have an idea to share?

Shopify API error when add new product

I'm using Shopify API with PHP CURL POST Method to create a product. Kindly find the following code and the response. I got error "Not Found".
<?php
$products_array = array(
"product"=>array(
'title'=>'',
"title"=> "Burton Custom Freestlye 151",
"body_html"=> "<strong>Good snowboard!</strong>",
"vendor"=> "Burton",
"product_type"=> "Snowboard",
"published"=> false ,
"variants"=>array(
array(
"sku"=>"t_009",
"price"=>20.00,
"grams"=>200,
"taxable"=>false,
)
)
)
);
echo json_encode($products_array);
echo "<br />";
$url = "https://apikey:password#hostname/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
?>
The response of the request :
{"product":{"title":"Burton Custom Freestlye 151","body_html":"Good snowboard!<\/strong>","vendor":"Burton","product_type":"Snowboard","published":false,"variants":[{"sku":"t_009","price":20,"grams":200,"taxable":false}]}}
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 01 Apr 2017 21:08:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block; report=/xss- report?source%5Baction%5D=create&source%5Bapp%5D=Shopify& source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin&source%5Buuid%5D=5539f94e-776f-4672-ab2a-f340c0cf2ad1
X-Dc: ash,chi2
X-Request-ID: 5539f94e-776f-4672-ab2a-f340c0cf2ad1
{"errors":"Not Found"}
How can I fix that error ? and what does that mean ??
Double check that you are using the right .myshopify.com hostname for your store. You can see this (along with the API key and password) after creating a private app in the Shopify admin: https://shopify.com/admin/apps/private

Send custom HTTP headers AND get response header - curl

I'm trying to curl (in PHP) a URL and send a custom header in the request. But then I also need to be able to view the response header that is returned. I'm querying an external API that I don't control.
I've tried using both the CURLOPT_HTTPHEADER and CURLOPT_HEADER options but they don't seem to work well together. CURLOPT_HEADER seems to overwrite the request headers so I can't authenticate my request BUT I then can view the headers in the response. If I take CURLOPT_HEADER out, I can successfully authenticate, but can't view headers.
PHP Code:
$url = "http://url-goes-here";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$headers = array("X-Auth-Token: $token");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
$out = curl_exec($ch);
curl_close($ch);
As per the documentation, it has to be
curl_setopt($ch, CURLOPT_HEADER, 1); // or TRUE
instead of
curl_setopt($ch, CURLOPT_HEADER, $headers);
EDIT: I must say I can't replicate the issue. I've created two scripts: a.php (using your content, with $url changed to 'http://localhost/b.php') and b.php (queried by a.php) which contains this:
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
So when I run php a.php, I get this:
HTTP/1.1 200 OK
Date: Tue, 16 Feb 2016 02:42:45 GMT
Server: Apache/2.4.10 (Fedora) PHP/5.6.15
X-Powered-By: PHP/5.6.15
Content-Length: 50
Content-Type: text/html; charset=UTF-8
Host: localhost
Accept: */*
X-Auth-Token: xxx
Which means I'm 1) getting the response headers successfully, and 2) receiving them successfully as well from a.php. I'd suggest you trying something similar and see if your web server (or your application) is playing tricks with you.

Error while trying to merge a pullrequest with bitbucket api

I get an error every time when I'm trying to merge a pull request with the bitbucket api.
This is my code:
define('USERNAME','***');
define('PASSWORD','***');
$url = "https://bitbucket.org/api/2.0/repositories/{owner}/{repo}/pullrequests/46/merge";
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl1, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);
curl_setopt($curl1, CURLOPT_HEADER, true);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_POST, true);
echo curl_exec($curl1);
And this is the error code:
HTTP/1.1 100 Continue HTTP/1.1 400 BAD REQUEST Server: nginx/1.6.2 Date: Wed, 20 May 2015 15:43:27 GMT Content-Type: application/json; charset=utf-8 Content-Length: 113 Connection: keep-alive X-Render-Time: 0.218739032745 Content-Language: de ETag: "2f0273bc2b819d7505bc14bf84d7e129" X-Request-Count: 227 X-Served-By: app19 Vary: Authorization, Accept-Language, Cookie X-Frame-Options: SAMEORIGIN X-Static-Version: 3b0c7aec39d3 X-Version: c288eef4a422 {"error": {"message": "'ascii' codec can't encode character u'\\xe4' in position 11: ordinal not in range(128)"}}
I've already tried to send the informations (owner, repo and request id) with "CURLOPT_POSTFIELDS". But I got the same error.
Can somebody help me?

Categories